微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用POI API在Java中访问受密码保护的Excel工作簿

我想读取和写入密码保护的Excel文件。 我怎样才能使用Apache POI API。

如何让Apache的configuration单元来处理多个客户端的查询

你如何处理在Apache共享服务器上的带宽计费?

如何解决HTTP 414“请求URI太长”的错误

PHP不parsing文件; 只是返回代码

VirtualHostconfiguration

POI应该能够打开保护的xls文件(使用org.apache.poi.hssf.record.crypt )和受保护的xlsx文件(使用org.apache.poi.poifs.crypt )。 你尝试过这些吗?

如果您使用的是hssf(用于xls文件),则需要在打开文件之前设置密码。 您可以通过以下方式致电:

org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(password);

之后,hssf应该能够打开你的文件

对于XSSF,你需要这样的东西:

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("protect.xlsx")); EncryptionInfo info = new EncryptionInfo(fs); Decryptor d = new Decryptor(info); d.verifyPassword(Decryptor.DEFAULT_PASSWORD); XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs));

或者,在较新版本的Apache POI中, WorkbookFactory支持在打开时提供密码 ,因此您可以执行如下操作:

Workbook wb = WorkbookFactory.create(new File("protected.xls"),"password"));

这对于hssf和XSSF都是有效的,根据格式选择合适的格式,并以适当的格式传递给定的密码

如果整个工作簿受密码保护(通过浏览Excel菜单文件>另存为…>工具>常规选项…然后提供密码),则该文件被加密,并且无法通过读取和写入工作簿POI。

码:

FileInputStream fileInput = new FileInputStream("2.xls"); BufferedInputStream bufferInput = new BufferedInputStream(fileInput); POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput); Biff8EncryptionKey.setCurrentUserPassword("test"); hssfWorkbook workbook = new hssfWorkbook(poiFileSystem,true);

使用的罐子:

公地编解码器1.5.jar

共享记录-1.1.jar

dom4j的-1.6.jar

jsr173_1.0_api.jar

基于JUnit 4.11.jar

log4j的-1.2.13.jar

POI-3.10-FINAL-20140208.jar

POI-excelant-3.10-FINAL-20140208.jar

POI-OOXML-3.10-FINAL-20140208.jar

POI-OOXML-模式-3.10-FINAL-20140208.jar

resolver.jar

xbean.jar

xbean_xpath.jar

XMLBeans的-qname.jar

xmlpublic.jar

官方文档链接详细提供了xls,xlsx和其他支持的格式的加密和解密可能性: http : //poi.apache.org/encryption.html

即在Binary Formats (xls)的情况下:

// XOR/RC4 decryption for xls Biff8EncryptionKey.setCurrentUserPassword("pass"); NPOIFSFileSystem fs = new NPOIFSFileSystem(new File("file.xls"),true); hssfWorkbook hwb = new hssfWorkbook(fs.getRoot(),true); Biff8EncryptionKey.setCurrentUserPassword(null);

对于基于XML的格式(xlsx):

EncryptionInfo info = new EncryptionInfo(filesystem); Decryptor d = Decryptor.getInstance(info); try { if (!d.verifyPassword(password)) { throw new RuntimeException("Unable to process: document is encrypted"); } InputStream dataStream = d.getDataStream(filesystem); // parse dataStream } catch (GeneralSecurityException ex) { throw new RuntimeException("Unable to process encrypted document",ex); }

Apache POI是excel处理的良好API。 但POI API只提供读取密码保护的Excel文件的选项。 无法使用密码写入excel文件

用apache poi创建一个密码保护的excel文件

检查这个线程知道更多关于密码保护的Excel。

try { InputStream is = new FileInputStream("myfile.xlsx"); POIFSFileSystem pfs = new POIFSFileSystem(is); EncryptionInfo info = new EncryptionInfo(pfs); Decryptor d = Decryptor.getInstance(info); if (!d.verifyPassword("passwordForFile")) { System.out.println("Incorrect password!"); } else { System.out.println("Correct password!"); //InputStream dataStream = d.getDataStream(pfs); // parse dataStream } } catch (Exception ex) { throw new RuntimeException("Unable to process encrypted document",ex); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐