PHPSpreadsheet如何密码保护?

5
使用 phpspreadsheet,有没有一种方法可以通过密码保护Excel表格,让用户在没有密码的情况下无法阅读?
我知道您可以保护单元格或工作表以防止写入,但我正在寻找一种方式来保护整个文件不被打开。一旦用户打开它,就会弹出“输入密码”屏幕。

如果您发现如何在 PHPSpreadsheet 中解锁工作簿,请告诉我 :) (Translated text: 如果您发现如何在 PHPSpreadsheet 中解锁工作簿,请告诉我 :)) - Barry Thomas
3个回答

1

因为phpspreadsheet没有实现此功能,但您可以先导出文件,然后使用https://github.com/nick322/secure-spreadsheet包来保护敏感的XLSX文件。

use Nick\SecureSpreadsheet\Encrypt;

$test = new Encrypt();
$test->input('Book1.xlsx')
    ->password('111')
    ->output('bb.xlsx');

0

设置电子表格的安全性

Excel提供了3个级别的“保护”:

  1. 文档:允许您在整个电子表格上设置密码,只有输入该密码才能进行更改。
  2. 工作表:提供其他安全选项:您可以禁止在特定工作表上插入行,禁止排序等。
  3. 单元格:提供锁定/解锁单元格以及显示/隐藏内部公式的选项。 设置文档安全性的示例:

    $spreadsheet->getSecurity()->setLockWindows(true); $spreadsheet->getSecurity()->setLockStructure(true); $spreadsheet->getSecurity()->setWorkbookPassword("PhpSpreadsheet");

设置工作表安全性的示例:

   $spreadsheet->getActiveSheet()->getProtection()->setPassword('PhpSpreadsheet');
   $spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
   $spreadsheet->getActiveSheet()->getProtection()->setSort(true);
   $spreadsheet->getActiveSheet()->getProtection()->setInsertRows(true);
   $spreadsheet->getActiveSheet()->getProtection()->setFormatCells(true);

设置单元格安全性的示例:

$spreadsheet->getActiveSheet()->
getStyle('B1')->
getProtection()->
setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_UNPROTECTED);

如果您需要工作表保护功能,请确保启用工作表保护!可以使用以下代码完成此操作:

$spreadsheet->getActiveSheet()->getProtection()->setSheet(true);

这只是防止文档/工作表/单元格的编辑。它不能阻止数据的查看,就像问题所述。 - Praemon

-3

在 Excel 2016 中(不同版本可能会有所不同)

打开您的电子表格并前往:

  • 文件 >
  • 信息 >
  • 保护工作簿 > 使用密码加密

输入密码,这将在用户打开时提示。


谢谢回复,我已经编辑了我的问题,因为我需要使用phpspreadsheet而不是在Excel本身中。 - Oren Havshush

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接