user3100533
user3100533

Reputation: 505

Protect the Excel file using PHPExcel

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.

Upvotes: 11

Views: 22909

Answers (3)

IamMHussain
IamMHussain

Reputation: 704

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');

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

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');

Upvotes: 15

Ruben
Ruben

Reputation: 343

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

Upvotes: 1

Related Questions