MNB PHPExcelDeveloper Guide
v2.0.5
Writing workbooks

Encrypt and Protect XLSX Output

Add password-to-open encryption, workbook structure protection, and worksheet editing restrictions.

Updated

LevelAdvancedReading time12 minPackagemnb/mnb-phpexcel
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#

ControlPurpose
File encryptionPrevents opening the XLSX without the password.
Workbook protectionRestricts structural changes such as adding or deleting sheets.
Worksheet protectionRestricts cell and sheet operations after opening.

Apply complete protection#

PHP
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#

PHP
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#