DanRz Posted January 24, 2023 Share Posted January 24, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/315834-phpspreadsheet-add-workbook-password/ Share on other sites More sharing options...
maxxd Posted January 24, 2023 Share Posted January 24, 2023 Looks like you can set a password on the document, sheet, or cell: https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-security-on-a-spreadsheet Quote Link to comment https://forums.phpfreaks.com/topic/315834-phpspreadsheet-add-workbook-password/#findComment-1604985 Share on other sites More sharing options...
DanRz Posted January 24, 2023 Author Share Posted January 24, 2023 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"); Quote Link to comment https://forums.phpfreaks.com/topic/315834-phpspreadsheet-add-workbook-password/#findComment-1604990 Share on other sites More sharing options...
DanRz Posted January 24, 2023 Author Share Posted January 24, 2023 Looks like PhpSpreadsheet can't actually protect the opening of a document... see https://github.com/PHPOffice/PhpSpreadsheet/issues/761 Which is odd because to me the documentation reads like it does... Quote Link to comment https://forums.phpfreaks.com/topic/315834-phpspreadsheet-add-workbook-password/#findComment-1604991 Share on other sites More sharing options...
Solution DanRz Posted January 24, 2023 Author Solution Share Posted January 24, 2023 https://github.com/nick322/secure-spreadsheet adding this repo works. Quote Link to comment https://forums.phpfreaks.com/topic/315834-phpspreadsheet-add-workbook-password/#findComment-1604996 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.