11

How can I create password protected excel sheet using PHPExcel, I know how to protect excel sheet using

$G=$objPHPExcel->setActiveSheetIndex(0);
$G->getProtection()->setSheet(true);

But I am not getting any link how to set the password for editing protection only, means user can open the file without password but cannot remove the protection from sheet which can be easily done by any one from Data menu. Suggestions are welcomed.

user3100533
  • 505
  • 2
  • 8
  • 20

3 Answers3

14

For Excel2007 Writer only:

Set workbook security:

$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);

$objPHPExcel->getSecurity()->setWorkbookPassword('secret');

Set worksheet security:

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);

$objPHPExcel->getActiveSheet()->getProtection()->setPassword('password');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • So just to verify, all protection **only** works on Excel2007? – shaneparsons Mar 15 '16 at 16:49
  • Doesn't work at all, the XLSX file still can be opened without password (PHPExcel 1.8) – Muhammad Abrar Dec 30 '16 at 01:06
  • 4
    @MuhammadAbrar - This works as it's supposed to work.... this doesn't protect the workbook from being opened; it protects elements of the worksheet from being changed.... understand what you're complaining about before you complain – Mark Baker Dec 30 '16 at 01:21
1

At the time, PHPExcel does not support protecting sheets with a password.

Ruben
  • 343
  • 2
  • 11
0

Try these options which are not mentioned in Documentation.

$objPHPExcel->getActiveSheet()->getProtection()->setSelectLockedCells(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSelectUnlockedCells(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertColumns(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertHyperlinks(true);
$objPHPExcel->getActiveSheet()->getProtection()->setDeleteColumns(true);
$objPHPExcel->getActiveSheet()->getProtection()->setDeleteRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setAutofilter(true);
$objPHPExcel->getActiveSheet()->getProtection()->setObjects(true);
$objPHPExcel->getActiveSheet()->getProtection()->setScenarios(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setPassword('password');
IamMHussain
  • 716
  • 8
  • 11