使用Apache POI读取受密码保护的XLSM文件

时间:2016-01-26 07:11:12

标签: java excel apache apache-poi xlsm

我正在使用以下代码。

File pt_file_obj = new File("file.xlsm");
FileInputStream pt_inp_strm_obj = new FileInputStream(pt_file_obj);
POIFSFileSystem poi_fs = new POIFSFileSystem(pt_inp_strm_obj);
EncryptionInfo info = new EncryptionInfo(poi_fs);
Decryptor d = Decryptor.getInstance(info);
if(!d.verifyPassword("mypassword")) {
      System.out.println("Incorrect password!");
}
Workbook wb = WorkbookFactory.create(d.getDataStream(poi_fs));

我在第POIFSFileSystem poi_fs = new POIFSFileSystem(pt_inp_strm_obj);

上遇到运行时错误

我不明白我做错了什么......

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:128)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:300)
    at ptaccess.PTAccess.main(PTAccess.java:20)
Java Result: 1

0 个答案:

没有答案
相关问题