Jump to content

PhpSpreadsheet - Add Workbook Password


Go to solution Solved by DanRz,

Recommended Posts

Hey,

I have the following script that looks to make an excel document using PHPSpreadsheet... the script works great, but it doesn't apply the password to the workbook... what am I missing?

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Information
$arrayData[] = array('cells');

// Make File Name
$filename = 'file-'.time().'.xlsx';

$spreadsheet = new Spreadsheet();

$spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
$security = $spreadsheet->getSecurity();
$security->setLockWindows(true);
$security->setLockStructure(true);
$security->setWorkbookPassword("PhpSpreadsheet");

$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Report Generated '. date(DATE_FORMAT_LONG));
$sheet->fromArray($arrayData, NULL, 'A3');

header('Content-Disposition: attachment;filename='.$filename);

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

Thanks

Dan

Link to comment
https://forums.phpfreaks.com/topic/315834-phpspreadsheet-add-workbook-password/
Share on other sites

I tried adding that code but it still doesn't seem to password protect the actual document...

Just unable to edit a sheet or cell...

$security = $spreadsheet->getSecurity();
$security->setLockWindows(true);
$security->setLockStructure(true);
$security->setWorkbookPassword("PhpSpreadsheet");

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.