使用 PHPSpreadsheet 打开受密码保护的 XLSX 文件
Open XLSX file that protected by a password with PHPSpreadsheet
我正在尝试打开受 PHPSpreadsheet (documentation 密码保护的 Excel 文件 (.xlsx)。我知道密码但是找不到打开方式
\PhpOffice\PhpSpreadsheet\Reader\Xlsx
的 load()
方法无法插入密码,当我尝试加载文件时 returns 出现错误(当然)。
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";
这里是错误
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
这段代码如何处理密码?
目前我不能尝试,但是我想你必须这样做:
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";
我不确定,明天我会去办公室,可以试试。
欢迎您查看我的 PHPDecryptXLSXWithPassword 存储库。
它也适用于 DOCX/PPTX 文件,但这个答案是针对您的问题的:首先 解密 带密码的文件,然后使用 PHPSpreadsheet 解密文件.
这是一个例子:
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";
注意:这是一个实验代码,请谨慎使用。不要在生产中使用!
我正在尝试打开受 PHPSpreadsheet (documentation 密码保护的 Excel 文件 (.xlsx)。我知道密码但是找不到打开方式
\PhpOffice\PhpSpreadsheet\Reader\Xlsx
的 load()
方法无法插入密码,当我尝试加载文件时 returns 出现错误(当然)。
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";
这里是错误
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
这段代码如何处理密码?
目前我不能尝试,但是我想你必须这样做:
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";
我不确定,明天我会去办公室,可以试试。
欢迎您查看我的 PHPDecryptXLSXWithPassword 存储库。
它也适用于 DOCX/PPTX 文件,但这个答案是针对您的问题的:首先 解密 带密码的文件,然后使用 PHPSpreadsheet 解密文件.
这是一个例子:
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";
注意:这是一个实验代码,请谨慎使用。不要在生产中使用!