使用 Apache POI 创建受密码保护的 Excel 文件? [英] Create a password protected Excel file using Apache POI?

查看:66
本文介绍了使用 Apache POI 创建受密码保护的 Excel 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的 Java 程序来使用 (Apache POI) API 创建 Excel 文件.我使用 Oracle 10g 作为数据库并使用 ojdbc14 JAR 文件.

I am developing a simple Java program to create an Excel file using (Apache POI) API. I am using Oracle 10g as a database and using the ojdbc14 JAR file.

我有一个名为 USERINFO 的表,它有三列,分别是 USERNAMEPASSWORDNAME.现在使用 Apache POI,我已经能够将所有行放在一个 Excel 文件中.

I have a table called USERINFO, having three columns, namely USERNAME, PASSWORD, and NAME. Now using Apache POI, I have been able to put all the rows in an Excel file.

由于该文件包含用户名和密码等敏感数据,我想对其进行密码保护.在论坛上,我找到了如何阅读受密码保护的文件,但没有找到如何创建它们.那么我如何才能做到这一点?

Since the file contains sensitive data such as username and password, I want to make it password protected. On forums, I have found how to read password protected files, but not how to create them. So how I can achieve this?

推荐答案

更新: 从 3.10 版开始,POI 支持 XLSX 文件的加密和解密.请参阅加密支持"POI 网站上的页面.以下内容仍与 XLS 二进制工作簿相关.

Updated: As of version 3.10 POI supports encryption as well as decryption for XLSX files. See the "Encryption Support" page on POI's website. The below is still relevant for XLS binary workbooks.

根据加密支持"POI 网站页面 POI 支持读取加密的XLS 和XLSX 文件.该页面上没有提到加密,这意味着它不受支持.这是通过搜索POI站点加密" 只返回少数结果,所有这些结果都与解密有关.我还查看了他们的加密实现的来源,它似乎只处理解密.这并不奇怪;POI 专为数据提取和搜索索引而设计,而不是用于创建新的电子表格.

According to the "Encryption Support" page on POI's website POI supports reading encrypted XLS and XLSX files. Encrypting is not mentioned on that page, which implies that it's not supported. This is backed up by searching the POI site for "encrypt" which returns only a handful of results all of which are about decryption. I've also taken a look at the sources for their crypto implementation, which appears to only handle decryption. This isn't surprising; POI is designed for data extraction and search indexing, not for creating new spreadsheets.

正如其他人所建议的,通常可以通过在 Excel 中创建模板,然后使用 POI 用数据填充它来解决 POI 中缺失的功能.不幸的是,这不适用于加密,因为加密电子表格的文件格式完全不同.

As others have suggested, it's often possible to work around missing features in POI by creating a template in Excel and then using POI to populate it with data. Unfortunately that won't work for encryption because the file format of encrypted spreadsheets is radically different.

如果你愿意为商业软件付费,最新版本的ExtenXLS 对 Excel 支持的所有加密格式具有完全的读写支持.只需构建一个 EncryptedWorkBookHandle 而不是普通的 WorkBookHandle.这将使用未经修改的 JRE、用于 XLS 的 RC4 和用于 XLSX 的 128 位 AES 支持的最强密码.如果您想在 OOXML 中使用 256 位 AES 并且您已经安装了 JCE 无限制策略,您可以使用 MSOfficeEncrypter 类来实现.

If you're willing to pay for commercial software, the latest version of ExtenXLS has full read and write support for all the encryption formats supported by Excel. Just construct an EncryptedWorkBookHandle instead of the normal WorkBookHandle. That will use the strongest possible cipher supported by an unmodified JRE, RC4 for XLS and 128-bit AES for XLSX. If you want to use 256-bit AES with OOXML and you've installed the JCE unlimited policy you can do so with the MSOfficeEncrypter class.

JExcelAPI 是一种流行的开源 Java 电子表格 API,似乎根本不支持加密.Aspose.Cells,一种商业产品, 支持强加密.Actuate 的 e.Spreadsheet 的文档似乎已经从网上消失了,所以我不知道它是否支持加密.

JExcelAPI, a popular open-source Java spreadsheet API, does not appear to support encryption at all. Aspose.Cells, a commercial offering, supports stong encryption. The documentation for Actuate's e.Spreadsheet seems to have disappeared from the 'net, so I can't tell whether it supports encryption or not.

由于免费提供的 Java 电子表格 API 似乎都不支持编写加密电子表格,如果您不愿意使用商业软件,则需要想出一个解决方法.例如,您可以将电子表格写入加密的 ZIP 文件.java.util.zip 不支持加密,但看起来像 Zip4j 确实如此.

Since none of the freely available Java spreadsheet APIs seems to support writing encrypted spreadsheets, if you're not willing to use commercial software you'll need to come up with a workaround. You could, for example, write the spreadsheet into an encrypted ZIP file. java.util.zip doesn't support encryption, but it looks like Zip4j does.

全面披露:我在 Extentech 工作,ExtenXLS 背后的公司.

Full disclosure: I work for Extentech, the company behind ExtenXLS.

这篇关于使用 Apache POI 创建受密码保护的 Excel 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆