首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从xlsm加密文件中读取?

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

Stack Overflow用户
提问于 2017-12-06 21:38:46
回答 1查看 929关注 0票数 1

我试图从用密码加密的xlsm Excel文件中读取数据。

到目前为止,我在Apache POI网站上找到的方法都没有成功,也没有堆栈溢出。

以下是我到目前为止所尝试的,以及我所得到的例外:

代码语言:javascript
运行
复制
    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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-10 16:18:57

促进对答案的评论..。

首先,您应该使用采用密码的WorkbookFactory.create方法,让Apache POI为您完成检测类型和设置解密的所有艰苦工作,例如

代码语言:javascript
运行
复制
Workbook wb = WorkbookFactory.create(new File("protected.xlsx"),"SecurePassword);

其次,到目前为止,您工作中的关键错误消息是:

代码语言:javascript
运行
复制
org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files

这是告诉您,您有一个有漏洞/步履蹒跚/故意损坏的JVM安装,它缺乏足够的密码支持来删除您所拥有的文件。正如错误解释的那样,您需要从JVM提供程序获取JVM的无限强JCE文件,并将它们安装到您的所有Java安装中,以便使用足够强的加密来匹配Excel使用的

此外,关于加密和解密的Apache页面可能也会派上用场!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47683873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档