I am trying to get users to save/download my excel file in read-only mode (apache on windows server, all users downloading are using windows).
So far, I have looked around for a good solution and have tried everything suggested but nothing seems to be working.
I tried adding following lines:
$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
but still it is saving in an editable mode. Am I missing something here?
I would prefer a solution where above mentioned goal is achieved by using phpexcel but the use of core php is also acceptable.
Something that requires a modification to phpexcel core class won't be acceptable as there's a whole bunch of applications developed by other developers in our organisation who assume core class to be untouched.
from the docs:
Document security allows you to set a password on a complete spreadsheet, allowing changes to be made only when that password is entered.
The example they give is as follows:
$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
$objPHPExcel->getSecurity()->setWorkbookPassword("PHPExcel");
it looks like you're missing the password.
I did not found any way without set password.
// Set password for readonly activesheet
$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
$objPHPExcel->getSecurity()->setWorkbookPassword("password");
// Set password for readonly data
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setPassword("password");
This code worked for me from editing the value of cells and formatting them.
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);