如何通过 SXSSF 保护 Excel 工作簿?

How to Protect Excel Workbook Via SXSSF?

我有一个程序可以生成包含大量数据的报告。我使用 Apache POI SXSSF 生成 xlsx 文件,使一切正常。 http://poi.apache.org/spreadsheet/index.html

我在他们的文档中找不到的是如何用密码保护 ENTIRE WORKBOOK。我想要它,以便如果有人试图打开文件,他们需要输入密码才能查看数据。

请记住,这不同于用密码保护单个工作表,后者仍然可以打开文件并查看数据,但只有只读权限。

我在 SXSSFWorkbook 文档中没有找到任何内容:https://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html

看起来 XSSFWorkbook 有一个名为 setWorkbookPassword 的方法,但 SXSSF 不存在该方法,并且在 SXSSFWorkbook 上不起作用。 https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html#setWorkbookPassword(java.lang.String,%20org.apache.poi.poifs.crypt.HashAlgorithm)

有人知道如何做到这一点吗?还将考虑替代解决方法。

提前致谢。

更新

我想过也许可以使用启用宏的工作簿和脚本来按照此处的建议对其进行密码保护。

我使用这里的 VBA 代码来做到这一点:http://analysistabs.com/excel-vba/protect-unprotect-workbook/ 然后在创建 Excel 文件时使用该文件作为模板,但是当我在玩弄事实证明它是不够的。某些计算机安全设置设置为 "High" 并将禁用宏,因此当我打开文件时,我确实收到了输入密码的提示,但随后我还收到一条警告消息,指出宏已被禁用,我可以查看工作簿内容。

有什么建议吗?

" ... I couldn't find in their documentation is how to password protect ..."???

你在 poi 主页的左侧菜单中看到那个菜单条目 "Encryption support" 了吗?

要在打开(即读取)文件时请求密码提示,您需要对其进行加密 - 请参阅 "XML-based formats - Encryption"

并且由于 Whosebug 喜欢将所有内容放在一个地方 - 这是代码:

// create a new POIFSFileSystem, which is the container for 
// encrypted OOXML-based files  
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);

// setup the encryption
Encryptor enc = info.getEncryptor();
enc.confirmPassword("foobaa");

// after writing to the SXSSF workbook you need the content
// as a stream
InputStream is = <open the SXSSF workbook as stream>
OutputStream os = enc.getDataStream(fs);
// copy the stream to the OutputStream
IOUtils.copy(is, os);

// Write out the encrypted version
FileOutputStream fos = new FileOutputStream("...");
fs.writeFilesystem(fos);
fos.close();