0

I am trying to create password protected excel file using PHP. Tried many options including PHPExcel library, but its not preventing from Excel from reading. Whenever user tries to open Excel, i need to ask password. I can not go with zipping the file and adding password to it, as suggested in many forums.

Is there any other option ?

4
  • Share some of the problematic code and we can help debug. Commented Oct 8, 2014 at 5:30
  • I didn't get any such code yet. I tried PHPExcel library but it prevents only file modification (i.e. user can still read file but can not modify without password). Commented Oct 8, 2014 at 5:37
  • My Sample Code : $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello'); $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'world!'); $objPHPExcel->getSecurity()->setLockWindows(true); $objPHPExcel->getSecurity()->setLockStructure(true); $objPHPExcel->getSecurity()->setWorkbookPassword('1234'); $objPHPExcel->getActiveSheet()->setTitle('Reports'); $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $objWriter->save('Reports.xlsx'); Commented Oct 8, 2014 at 5:38
  • If I remember correctly PhpSpreadsheet only supports protecting data changes, not reading. If you need something like that, you'd probably need to go the password-protected zip route. You can try someone within the PhpSpreadsheet community knows more. github.com/Maatwebsite/Laravel-Excel/issues/3152 github.com/Maatwebsite/Laravel-Excel/discussions/3151 Commented May 12, 2021 at 7:56

3 Answers 3

1

PHPExcel does not support writing password-protected files so that they cannot be read without entering the password; the password protection features in PHPExcel are only to prevent editing of data, changing the structure of the worksheets, etc

Sign up to request clarification or add additional context in comments.

2 Comments

I'll take your word for it then.... I wasn't aware that I'd implemented password protection for opening files
If I remember correctly PhpSpreadsheet only supports protecting data changes, not reading. If you need something like that, you'd probably need to go the password-protected zip route. You can try someone within the PhpSpreadsheet community knows more. github.com/Maatwebsite/Laravel-Excel/issues/3152 github.com/Maatwebsite/Laravel-Excel/discussions/3151
-2

There is a duplicate of It I hope?

Go Here

$reader = new PHPExcel_Reader_Excel2007;
$workbook =  $reader->load("document.xlsx");
$workbook->-getActiveSheet()->getSecurity()->setWorkbookPassword("your password");

2 Comments

Thanks for response. I already tried this, but it prevents only file modification.
If I remember correctly PhpSpreadsheet only supports protecting data changes, not reading. If you need something like that, you'd probably need to go the password-protected zip route. You can try someone within the PhpSpreadsheet community knows more. github.com/Maatwebsite/Laravel-Excel/issues/3152 github.com/Maatwebsite/Laravel-Excel/discussions/3151
-2

Try this

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

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

for worksheet security:it is working for me!

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

but for work book security,phpExcel have a issue i think, still exploring

1 Comment

No dear nothing related to os.Because i am using windows and when trying to do this the worksheet is working fine but the workbook password is not working.your approaching is correct.once try for the worksheet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.