我们如何使用 POI API 读取受保护的密码 excel 文件 (.xls)
我刚刚学习了 POI,发现 HSSF 的读取和创建 excel 文件(.xls)非常简单。 但是,当我想读取受密码保护的Excel时,我发现了一些问题。 我花了一个小时在互联网上找到这个解决方案。
请你帮我解决这个问题。 如果您能给我一个代码片段,我非常高兴。
谢谢。
I've just learned POI and find the HSSF is very simple to read and create excel file (.xls).
However, I found some problem when want to read excel protected with password.
It took me an hour to find this solution on internet.
Please could you help me to solve this problem.
I'm very glad if you could give me a code snippet.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅 http://poi.apache.org/encryption.html - 如果您使用的是足够新的 Apache 副本POI(例如3.8)然后加密的.xls文件(HSSF)和.xlsx文件(XSSF)可以解密(证明你有密码!)
但目前你无法写出加密的excel文件,只能写出未加密的文件
See http://poi.apache.org/encryption.html - if you're using a recent enough copy of Apache POI (eg 3.8) then encrypted .xls files (HSSF) and .xlsx files (XSSF) can be decrypted (proving you have the password!)
At the moment you can't write out encrypted excel files though, only un-encrypted ones
在您写下问题时,使用 Apache POI 并不容易。 从那时起,支持已经取得了长足进步
。如今,如果您想打开受密码保护的 Excel 文件,无论是
.xls
还是.xlsx
,您都知道该文件的密码,您需要做的就是使用 WorkbookFactory.create(File,Password),例如这将识别文件的类型,使用给定的密码对其进行解密,然后为您打开它。 然后您就可以正常阅读内容了
At the time you wrote your question, it wasn't easy to do with Apache POI. Since then, support has come on a long way
These days, if you want to open a password protected Excel file, whether
.xls
or.xlsx
, for which you know the password, all you need to do is use WorkbookFactory.create(File,Password), egThat'll identify the type of the file, decrypt it with the given password, and open it for you. You can then read the contents as normal
这是一个完整的示例代码,它读入受保护的 Excel 文件,使用密码解密并写出不受保护的 Excel 文件
Here is a complete example code that reads in a protected excel file, decrypts using a password and writes out unprotected excel file
这是读取 Excel 文件的代码,检查 .xls 和 .xlsx(有密码保护或没有密码保护)作为完整的示例代码。
That is the code for Read Excel file with checking of .xls and .xlsx (with password protected or without password protected) as complete example code.
POI 将无法读取加密的工作簿 - 这意味着如果您保护了整个工作簿(而不仅仅是一个工作表),那么它将无法阅读。 否则,它应该有效。
POI will not be able to read encrypted workbooks - that means that if you have protected the entire workbook (and not just a sheet), then it won't be able to read it. Otherwise, it should work.
拉维是对的。 看来您可以读取受密码保护的文件,但不能读取 POI 加密的文件。 请参阅http://osdir.com/ml/user -poi.apache.org/2010-05/msg00118.html。 以下代码打印出文件的痕迹
如果您得到提及加密的输出,则无法使用 POI 打开该文件。
Ravi is right. It seems you can read password protected, but not encrypted files with POI. See http://osdir.com/ml/user-poi.apache.org/2010-05/msg00118.html. The following code prints out a trace of the file
If you get an output mentioning encryption then you cannot open the file with POI.