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
01Prevent spreadsheet formula injectionScan untrusted rows for formula-like text, escape dangerous CSV cells, and block risky explicit formulas before writing.
MediumDirect API+
Scan untrusted rows for formula-like text, escape dangerous CSV cells, and block risky explicit formulas before writing.
MnbExcel::scanCells()cellSafety()formulaPolicy()MnbExcel::text()<?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');02Detect, encrypt, and decrypt XLSX filesDetect encrypted Office containers and protect or recover an XLSX package with an application-supplied password.
AdvancedDirect API+
Detect encrypted Office containers and protect or recover an XLSX package with an application-supplied password.
MnbExcel::isEncryptedXlsx()MnbExcel::xlsxEncryptionMode()MnbExcel::encryptXlsx()MnbExcel::decryptXlsx()<?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);03Protect SheetApply worksheet editing restrictions while keeping the file readable.
EasyDirect API+
Apply worksheet editing restrictions while keeping the file readable.
protectSheet()<?php
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->protectSheet('Sheet1', 'EditPassword!', [
'selectLockedCells' => true,
'selectUnlockedCells' => true,
'formatCells' => false,
])
->save('protected-sheet.xlsx');04Protect WorkbookProtect workbook structure and optionally encrypt the entire file with a password to open.
EasyDirect API+
Protect workbook structure and optionally encrypt the entire file with a password to open.
protectWorkbook()protectAllSheets()encryptWithPassword()<?php
use Mnb\PHPExcel\MnbExcel;
MnbExcel::report($rows)
->protectWorkbook('StructurePassword!')
->protectAllSheets('EditPassword!')
->encryptWithPassword('OpenPassword!')
->save('secure-workbook.xlsx');Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue