Open XLSX file that protected by a password with PHPSpreadsheet

戏子无情 提交于 2021-02-19 01:23:46

问题


I'm trying to open an Excel file (.xlsx) that is protected by a password with PHPSpreadsheet (documentation). I know the password but I don't find a way to open it.

The load()method of \PhpOffice\PhpSpreadsheet\Reader\Xlsx doesn't give the possibility to insert a password and when I try to load the file it returns an error (of course).

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load('hello world.xlsx');
$sheet = $spreadsheet->getActiveSheet();
echo $sheet->getCell('A1')->getValue() . "\n";

And here is the error

Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311 Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313 Warning: Invalid argument supplied for foreach() in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 350 Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311 Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313 Warning: Invalid argument supplied for foreach() in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 397 Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311 Warning: ZipArchive::getFromName(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 313 Warning: Invalid argument supplied for foreach() in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1855 Warning: ZipArchive::close(): Invalid or uninitialized Zip object in /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 1883

How can this code deal with passwords?


回答1:


At this moment i can't try but, I suppose that you have to do something like this:

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load('hello world.xlsx');
$sheet = $spreadsheet->getActiveSheet();
$sheet->getProtection()->setSheet(true);
$sheet->getProtection()->setPassword('THEPASSWORD');
echo $sheet->getCell('A1')->getValue() . "\n";

I'm not sure, tomorrow I will be to the office and can try.




回答2:


I welcome you to check out my PHPDecryptXLSXWithPassword repo.

It also works for DOCX/PPTX files, but this answer is specific to your question: first decrypt the file with password, and then use the decrypted file with PHPSpreadsheet.

Here is an example:

require_once('PHPDecryptXLSXWithPassword.php');

$encryptedFilePath = 'hello world.xlsx';
$password = 'mypassword'; // password to "open" the file
$decryptedFilePath = 'temp_path_to_decrypted_file.xlsx';

decrypt($encryptedFilePath, $password, $decryptedFilePath);

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($decryptedFilePath);
$sheet = $spreadsheet->getActiveSheet();
echo $sheet->getCell('A1')->getValue() . "\n";

Note: This is an experimental code, so use with caution. DO NOT use in production!



来源:https://stackoverflow.com/questions/49941725/open-xlsx-file-that-protected-by-a-password-with-phpspreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!