PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

Password Protection not working for XLSX

Open rutvikO2script opened this issue 3 years ago • 4 comments

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');
}
?>

rutvikO2script avatar Apr 25 '22 12:04 rutvikO2script

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?

oleibman avatar Apr 25 '22 14:04 oleibman

Not getting the expected result in any way.

rutvikO2script avatar Apr 26 '22 09:04 rutvikO2script

Can you send me working example of password protected excel file

rutvikO2script avatar Apr 26 '22 09:04 rutvikO2script

issue.2777.xlsx

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".'

oleibman avatar Apr 26 '22 14:04 oleibman

Four months, no update, no reason to think issue isn't a request for something that PhpSpreadsheet cannot handle. Closing.

oleibman avatar Sep 02 '22 02:09 oleibman