我正在尝试从用密码加密的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
答案 0 :(得分:3)
宣传回答评论......
首先,您应该使用WorkbookFactory.create method which takes a password,让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
这告诉你,你有一个nobbled / hobbled /故意部分破解的JVM安装,缺乏足够的加密支持来解密你已经获得的文件。正如错误所解释的那样,您需要从JVM提供程序获取JVM的无限强度JCE文件,并将它们安装到您的所有Java安装中,以使用足够强大的加密来匹配Excel使用的
此外,Apache POI page on Encryption and Decryption也可能派上用场了!