Encrypt and Protect XLSX Output
Add password-to-open encryption, workbook structure protection, and worksheet editing restrictions.
Updated
encryptWithPassword()protectWorkbook()protectSheet()protectAllSheets()passwordProtectOutput()What you will learn
- Choose encryption separately from editing protection.
- Generate Agile AES-256 password-to-open XLSX files.
- Apply workbook and worksheet restrictions intentionally.
Understand the three controls#
| Control | Purpose |
|---|---|
| File encryption | Prevents opening the XLSX without the password. |
| Workbook protection | Restricts structural changes such as adding or deleting sheets. |
| Worksheet protection | Restricts cell and sheet operations after opening. |
Apply complete protection#
MnbExcel::report($rows)
->protectWorkbook('EditPassword!', [
'lock_structure' => true,
])
->protectAllSheets('EditPassword!')
->encryptWithPassword('OpenPassword!', [
'mode' => 'agile',
'spin_count' => 100000,
])
->save('secure-report.xlsx');Use the convenience method for common output#
MnbExcel::report($rows)
->passwordProtectOutput('OpenPassword!', [
'encryption_options' => ['mode' => 'agile'],
'protect_workbook' => true,
'protect_sheets' => true,
'protection_options' => [
'lock_structure' => true,
],
])
->save('protected-report.xlsx');Handle passwords as secrets#
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue