MNB PHPExcelDeveloper Guide
v2.0.5

Security examples

Security Excel examples, explained clearly.

Open a focused example to see the implementation path, copy-ready PHP, expected workbook behavior, and the production boundary that matters.

4examples in this category
21focused categories
5implementation paths
01
Prevent spreadsheet formula injection

Scan untrusted rows for formula-like text, escape dangerous CSV cells, and block risky explicit formulas before writing.

MediumDirect API
MnbExcel::scanCells()cellSafety()formulaPolicy()MnbExcel::text()
prevent-spreadsheet-formula-injection.php
<?php

use Mnb\PHPExcel\MnbExcel;

$rows = [
    [
        'name' => 'Ava',
        'phone' => MnbExcel::text('0987654321'),
        'note' => '=HYPERLINK("https://evil.example","Open")',
    ],
];

$scan = MnbExcel::scanCells($rows);

if ($scan['total_issues'] > 0) {
    print_r($scan['issues']);
}

MnbExcel::fromArray($rows)
    ->withHeader()
    ->cellSafety(['max_text_length' => 32767])
    ->formulaPolicy('safe')
    ->save(__DIR__ . '/output/safe-contacts.csv');
02
Detect, encrypt, and decrypt XLSX files

Detect encrypted Office containers and protect or recover an XLSX package with an application-supplied password.

AdvancedDirect API
MnbExcel::isEncryptedXlsx()MnbExcel::xlsxEncryptionMode()MnbExcel::encryptXlsx()MnbExcel::decryptXlsx()
detect-encrypt-and-decrypt-xlsx-files.php
<?php

use Mnb\PHPExcel\MnbExcel;

$plain = __DIR__ . '/fixtures/payroll.xlsx';
$encrypted = __DIR__ . '/output/payroll-encrypted.xlsx';
$decrypted = __DIR__ . '/output/payroll-decrypted.xlsx';
$password = (string) getenv('PAYROLL_XLSX_PASSWORD');

if ($password === '') {
    throw new RuntimeException('PAYROLL_XLSX_PASSWORD is required.');
}

MnbExcel::encryptXlsx($plain, $encrypted, $password);

printf(
    "Encrypted: %s (%s)\n",
    MnbExcel::isEncryptedXlsx($encrypted) ? 'yes' : 'no',
    MnbExcel::xlsxEncryptionMode($encrypted) ?? 'unknown'
);

MnbExcel::decryptXlsx($encrypted, $decrypted, $password);
MnbExcel::assertValidXlsx($decrypted);
03
Protect Sheet

Apply worksheet editing restrictions while keeping the file readable.

EasyDirect API
protectSheet()
protect-sheet.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->protectSheet('Sheet1', 'EditPassword!', [
        'selectLockedCells' => true,
        'selectUnlockedCells' => true,
        'formatCells' => false,
    ])
    ->save('protected-sheet.xlsx');
04
Protect Workbook

Protect workbook structure and optionally encrypt the entire file with a password to open.

EasyDirect API
protectWorkbook()protectAllSheets()encryptWithPassword()
protect-workbook.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::report($rows)
    ->protectWorkbook('StructurePassword!')
    ->protectAllSheets('EditPassword!')
    ->encryptWithPassword('OpenPassword!')
    ->save('secure-workbook.xlsx');