Apache POI"无效的标题签名"打开Excel XML

时间:2016-07-12 16:27:21

标签: java xml excel apache-poi spreadsheetml

是否可以打开一个excel xml(一个可以在microsoft office excel中打开的xml),它是一个非常好的xml,符合xml spreadsheet reference

我试图使用:

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)

1 个答案:

答案 0 :(得分: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);