Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open XLSX file that protected by a password with PHPSpreadsheet

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?

like image 393
B 7 Avatar asked Apr 20 '18 12:04

B 7


1 Answers

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!

like image 180
Jay Dadhania Avatar answered Nov 14 '22 23:11

Jay Dadhania