如何获取 SignatureLines excel apache poi

How to get SignatureLines excel apache poi

早上好,

我创建了一个带有签名行的excel。

我正在尝试使用 apache poi 库在 excel 文档中获取签名行。

XSSFWorkbook w = new XSSFWorkbook(mp.getInputStream()); w.get......?

有什么建议吗?

提前致谢,

巴勃罗

我看到有一个名为 XSSFSignatureLine 的 class,但我没有看到任何使用它的示例。

到get/set一个签名行from/into一个Excelsheet,apache poi在当前apache poi 5.x中引入了XSSFSignatureLine .此 class 提供了一种方法 parse(XSSFSheet sheet),每个 sheet 获得一个签名行。好像 apache poi 没想到每个 sheet.

可以有多个签名行

签名的文本数据存储在 VML 绘图中。因此,如果只是从签名行中获取文本数据,那么可以获取 sheet 的 VML 图和 select 来自 XML 的数据。当然签名行的二进制数据不能从XML.

得到

以下代码示例显示了这两种方法。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;

class ExcelGetSignatureLines {
    
 static void getSignatureLines(XSSFSheet sheet) throws Exception {
  XSSFSignatureLine signatureLine = new XSSFSignatureLine();
  signatureLine.parse(sheet);
  System.out.println("Found XSSFSignatureLine:");
  System.out.println(signatureLine.getSuggestedSigner());
  System.out.println(signatureLine.getSuggestedSigner2());
  System.out.println(signatureLine.getSuggestedSignerEmail());
 }
 
 static void getSignatureLinesFromVMLDrawing(XSSFSheet sheet) throws Exception {
  XSSFVMLDrawing vmlDrawing = sheet.getVMLDrawing(false);
  if (vmlDrawing != null) {
   org.apache.poi.schemas.vmldrawing.XmlDocument vmlDrawingDocument = vmlDrawing.getDocument();
   String declareNameSpaces = "declare namespace v='urn:schemas-microsoft-com:vml'; " 
                            + "declare namespace o='urn:schemas-microsoft-com:office:office' ";
   org.apache.xmlbeans.XmlObject[] selectedObjects = vmlDrawingDocument.selectPath(
    declareNameSpaces 
    + ".//v:shape//o:signatureline");
   for (org.apache.xmlbeans.XmlObject object : selectedObjects) {
    if (object instanceof com.microsoft.schemas.office.office.CTSignatureLine) {
     com.microsoft.schemas.office.office.CTSignatureLine ctSignatureLine = (com.microsoft.schemas.office.office.CTSignatureLine)object;
     System.out.println("Found CTSignatureLine:");
     System.out.println(ctSignatureLine.getSuggestedsigner());
     System.out.println(ctSignatureLine.getSuggestedsigner2());
     System.out.println(ctSignatureLine.getSuggestedsigneremail());
    }
   }  
  }
 }
 
 public static void main(String[] args) throws Exception {
  Workbook workbook = WorkbookFactory.create(new FileInputStream("./WorkbookHavingSignatureLines.xlsx"));
  for (Sheet sheet : workbook ) {
   if (sheet instanceof XSSFSheet) {
    System.out.println("Sheet " + sheet.getSheetName());
    getSignatureLines((XSSFSheet)sheet);
    getSignatureLinesFromVMLDrawing((XSSFSheet)sheet);
   }
  }
  workbook.close();
 }
}