gpt4 book ai didi

java - 如何从 xlsm 加密文件中读取?

转载 作者:行者123 更新时间:2023-11-29 04:23:30 30 4
gpt4 key购买 nike

我正在尝试从用密码加密的 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

最佳答案

将评论提升为答案...

首先,您应该使用 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

这告诉您,您有一个高贵的/笨拙的/故意部分损坏的 JVM 安装,它缺乏足够的加密支持来解密您拥有的文件。正如错误所解释的那样,您需要从 JVM 提供商处为您的 JVM 获取无限强度的 JCE 文件,并将它们安装到您所有的 Java 安装中,以便使用足够强大的加密来匹配 Excel 使用的内容

此外,Apache POI page on Encryption and Decryption也可能对您有用!

关于java - 如何从 xlsm 加密文件中读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47683873/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com