DanRz Posted January 24 Share Posted January 24 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 Share on other sites More sharing options...
maxxd Posted January 24 Share Posted January 24 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 Share on other sites More sharing options...
DanRz Posted January 24 Author Share Posted January 24 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 Share on other sites More sharing options...
DanRz Posted January 24 Author Share Posted January 24 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 Share on other sites More sharing options...
Solution DanRz Posted January 24 Author Solution Share Posted January 24 https://github.com/nick322/secure-spreadsheet adding this repo works. Quote Link to comment 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.