PhpSpreadsheet
PhpSpreadsheet copied to clipboard
Password Protection not working for XLSX
This is:
- [*] a bug report
What is the expected behavior?
Expected behavior should be generated password-protected file
What is the current behavior?
Currently not generating the password-protected file.
Steps to reproduce?
<?php
use PhpOffice\PhpSpreadsheet\Worksheet\Protection;
require_once('vendor/autoload.php');
try
{
convert('ninja-forms-submission.csv');
} catch(Exception $e) {
echo $e->getMessage();
}
function convert($csv_file) {
$file = basename($csv_file);
$filename = basename($csv_file, ".csv");
$path = str_replace($file,$filename.rand(1,9999999999999999).'.xlsx',$csv_file);
$reader = new PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load($csv_file);
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save($path);
$wreader = PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$workbook = $wreader->load($path);
// $workbook->getActiveSheet()->getSecurity()->setWorkbookPassword("123456");
$workbook->getSecurity()->setLockWindows(true);
$workbook->getSecurity()->setLockStructure(true);
$workbook->getSecurity()->setWorkbookPassword('secret');
}
?>
I do not understand your example. You are setting the password and the locks after you save the workbook. Do you not get the expected results when you set them before the save?
Not getting the expected result in any way.
Can you send me working example of password protected excel file
This file was essentially created with your code, except that the security options are applied before the save. Note that you cannot insert or delete sheets (lock structure) nor hide the worksheet (lock windows) without specifying the password.
If, by password protection, you mean not being able to open the file at all without knowing the password, PhpSpreadsheet does not do that. This is in the documentation: 'If you need to completely prevent reading a file by any tool, including PhpSpreadsheet, then you are looking for "encryption", not "protection".'
Four months, no update, no reason to think issue isn't a request for something that PhpSpreadsheet cannot handle. Closing.