我试图从用密码加密的xlsm Excel文件中读取数据。
到目前为止,我在Apache POI网站上找到的方法都没有成功,也没有堆栈溢出。
以下是我到目前为止所尝试的,以及我所得到的例外:
String fileName = "C:\\encryptedExcel.xlsm";
String password = "passcode!";
try {
// XOR/RC4 decryption for xls
Biff8EncryptionKey.setCurrentUserPassword(password);
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(fileName), true);
HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
}
catch(Exception e) {e.printStackTrace();System.err.println(e);}
//org.apache.poi.EncryptedDocumentException: The supplied spreadsheet seems to be an Encrypted .xlsx file. It must be decrypted before use by XSSF, it cannot be used by HSSF
try {
Biff8EncryptionKey.setCurrentUserPassword(password);
Workbook workbook = new XSSFWorkbook(OPCPackage.open(fileName,PackageAccess.READ));
Biff8EncryptionKey.setCurrentUserPassword(null);
}
catch(Exception e) {e.printStackTrace();System.err.println(e);}
//org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
//if (!bis.markSupported()) {is = new PushbackInputStream(bis, 8);}
if (POIFSFileSystem.hasPOIFSHeader(bis)) {
POIFSFileSystem fs = new POIFSFileSystem(bis);
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(password);
//is = d.getDataStream(fs);
}
}
catch(Exception e) {e.printStackTrace();}
//throws java.io.IOException: getFileMagic() only operates on streams which support mark(int)
//fixed by converting to BufferedInputStream
//then throws org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files
try {
File input = new File(fileName);
Workbook wb = WorkbookFactory.create(input, password);
}
catch(Exception e) {e.printStackTrace();System.err.println(e);}
//org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files
发布于 2017-12-10 16:18:57
促进对答案的评论..。
首先,您应该使用采用密码的WorkbookFactory.create方法,让Apache POI为您完成检测类型和设置解密的所有艰苦工作,例如
Workbook wb = WorkbookFactory.create(new File("protected.xlsx"),"SecurePassword);
其次,到目前为止,您工作中的关键错误消息是:
org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files
这是告诉您,您有一个有漏洞/步履蹒跚/故意损坏的JVM安装,它缺乏足够的密码支持来删除您所拥有的文件。正如错误解释的那样,您需要从JVM提供程序获取JVM的无限强JCE文件,并将它们安装到您的所有Java安装中,以便使用足够强的加密来匹配Excel使用的
此外,关于加密和解密的Apache页面可能也会派上用场!
https://stackoverflow.com/questions/47683873
复制相似问题