0

是否可以打开一个excel xml(可以在microsoft office excel中打开的xml),它是一个非常好的xml,符合xml电子表格参考

我尝试使用:

String outputFile = "output.xml";
FileInputStream file = new FileInputStream( new File( outputFile ) );
Workbook wb = WorkbookFactory.create( new POIFSFileSystem( file ) );
Sheet sheet = wb.getSheetAt(0);

它向我显示了以下异常:

java.io.IOException: Invalid header signature; read 0x6576206C6D783F3C, expected 0xE11AB1A1E011CFD0
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
4

1 回答 1

0

似乎在较新的 Excel 版本中经常使用加密。对我来说,以下代码有效

InputStream fin = new FileInputStream(xlsFile);
BufferedInputStream in = new BufferedInputStream(fin);
if (POIFSFileSystem.hasPOIFSHeader(in)) {
    // if there is any encryption
    POIFSFileSystem fs = new POIFSFileSystem(in);
    EncryptionInfo info = new EncryptionInfo(fs);
    Decryptor d = Decryptor.getInstance(info);
    d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
    workbook = WorkbookFactory.create(d.getDataStream(fs));
}
else
    // without encryption
    workbook = WorkbookFactory.create(in);
于 2016-07-12T16:38:13.120 回答