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
21supported categories
5implementation paths
Monolithic library
Security examples with copy-ready, production-minded PHP.
Each example identifies the exact methods it uses and can be opened independently from the right-side index.
- 4
- examples
- 12
- documented methods
MnbExcel::scanCells()cellSafety()formulaPolicy()MnbExcel::text()<?php
declare(strict_types=1);
/*
Summary:
Scan untrusted rows for formula-like text, escape dangerous CSV cells, and block risky explicit
formulas before writing.
Implementation note:
Explicit formulas created with MnbExcel::formula() are checked by the formula guard.
Formula-like untrusted text is escaped according to the output policy.
*/
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' => 32_767])
->formulaPolicy('safe')
->save(__DIR__ . '/output/safe-contacts.csv');MnbExcel::isEncryptedXlsx()MnbExcel::xlsxEncryptionMode()MnbExcel::encryptXlsx()MnbExcel::decryptXlsx()<?php
declare(strict_types=1);
/*
Summary:
Detect encrypted Office containers and protect or recover an XLSX package with an
application-supplied password.
Implementation note:
Keep passwords outside source control and logs. Encryption protects the package at rest;
application authorization and secure delivery are still required.
*/
use Mnb\PHPExcel\MnbExcel;
$plain = __DIR__ . '/fixtures/payroll.xlsx';
if (!is_file($plain)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $plain));
}
$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);protectSheet()<?php
declare(strict_types=1);
/*
Summary:
Apply worksheet editing restrictions while keeping the file readable.
Implementation note:
Worksheet protection discourages editing; it is not equivalent to file encryption.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->protectSheet('Sheet1', 'EditPassword!', [
'selectLockedCells' => true,
'selectUnlockedCells' => true,
'formatCells' => false,
])
->save('protected-sheet.xlsx');protectWorkbook()protectAllSheets()encryptWithPassword()<?php
declare(strict_types=1);
/*
Summary:
Protect workbook structure and optionally encrypt the entire file with a password to open.
Implementation note:
Use separate strong passwords for open-file encryption and editing restrictions. Store secrets
outside source control.
*/
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->protectWorkbook('StructurePassword!')
->protectAllSheets('EditPassword!')
->encryptWithPassword('OpenPassword!')
->save('secure-workbook.xlsx');No matching example found.Try a method name, task, or result.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue