5

With phpspreadsheet

Is there a way to protect the Excel sheet with a password so users cannot read without the password?

I know you can protect a cell or a sheet from writing but I am looking for a way to protect the entire file from being open, once the user open it, it will pop up the "enter password" screen

Oren Havshush
  • 76
  • 1
  • 8
  • https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#setting-security-on-a-spreadsheet If you happen to figure out how to unlock a workbook with PHPSpreadsheet, please let me know :) – Barry Thomas Apr 27 '18 at 15:33

3 Answers3

0

Setting security on a spreadsheet

Excel offers 3 levels of "protection":

  1. Document: allows you to set a password on a complete spreadsheet, allowing changes to be made only when that password is entered.
  2. Worksheet: offers other security options: you can disallow inserting rows on a specific sheet, disallow sorting, ...
  3. Cell: offers the option to lock/unlock a cell as well as show/hide the internal formula. An example on setting document security:

    $spreadsheet->getSecurity()->setLockWindows(true); $spreadsheet->getSecurity()->setLockStructure(true); $spreadsheet->getSecurity()->setWorkbookPassword("PhpSpreadsheet");

An example on setting worksheet security:

   $spreadsheet->getActiveSheet()->getProtection()->setPassword('PhpSpreadsheet');
   $spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
   $spreadsheet->getActiveSheet()->getProtection()->setSort(true);
   $spreadsheet->getActiveSheet()->getProtection()->setInsertRows(true);
   $spreadsheet->getActiveSheet()->getProtection()->setFormatCells(true);

An example on setting cell security:

$spreadsheet->getActiveSheet()->
getStyle('B1')->
getProtection()->
setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_UNPROTECTED);

Make sure you enable worksheet protection if you need any of the worksheet protection features! This can be done using the following code:

$spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
  • This only prevents editing of the document/worksheet/cell. It does not prevent viewing of the data, as per the question. – Praemon Dec 19 '21 at 11:46
0

Because phpspreadsheet does not implement this function, but you can export the file first and then use https://github.com/nick322/secure-spreadsheet package to protect sensitive XLSX files.

use Nick\SecureSpreadsheet\Encrypt;

$test = new Encrypt();
$test->input('Book1.xlsx')
    ->password('111')
    ->output('bb.xlsx');
nick huang
  • 131
  • 1
  • 6
-3

On Excel 2016 ( mileage may vary between versions )

Open your spreadsheet and go to:

  • File >
  • Info >
  • Protect Workbook > Encrypt With Password

And put in a password, this will prompt the user when opening.

mquinn
  • 454
  • 4
  • 14