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.

3 Answers 3

15

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');
Sign up to request clarification or add additional context in comments.

3 Comments

So just to verify, all protection only works on Excel2007?
Doesn't work at all, the XLSX file still can be opened without password (PHPExcel 1.8)
@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
1

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

1 Comment

then how to set password in excel file to avoid open without password in php
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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.