XLSX Inspection and Security
Inspect file and sheet information without loading rows, then validate encryption, uploads, and package integrity.
Updated
ReadSession::inspect()ReadSession::sheetNames()ReadSession::countRows()Xlsx::isEncrypted()MnbExcel::validateXlsx()What you will learn
- Build a lightweight upload preview before reading records.
- Choose declared or accurate row-count modes.
- Distinguish encrypted files, unsafe uploads, and invalid packages.
Inspect workbook metadata without row arrays#
$session = MnbExcel::read('orders.xlsx');
$inspection = $session->inspect();
$sheetNames = $session->sheetNames();
$orders = null;
foreach ($inspection['sheets'] as $sheet) {
if ($sheet['name'] === 'Orders') {
$orders = $sheet;
break;
}
}
$filledRows = $session
->sheet('Orders')
->withHeaderRow()
->countRows(['skip_empty_rows' => true]);inspect() reads package structure, workbook relationships, worksheet dimensions, visibility, warnings, and security signals without materializing row arrays. countRows() then streams normalized rows for the selected worksheet when an application needs a populated-record count.
Choose a row-count meaning#
| Mode | Meaning | Cost |
|---|---|---|
declared_last_row | Last row from the worksheet dimension stored in inspection metadata. | Fastest; relies on producer metadata. |
row_tag_count | Physical <row> elements found in worksheet XML. | Fast package inspection. |
countRows() | Rows yielded by the normal reader after header, empty-row, range, and projection options. | Streams cell data; does not build a complete row array. |
Detect and handle encryption#
if (Xlsx::isEncrypted('finance.xlsx')) {
printf("Mode: %s\n", Xlsx::encryptionMode('finance.xlsx') ?? 'unknown');
}
$rows = MnbExcel::read('finance.xlsx', [
'password' => getenv('XLSX_PASSWORD'),
])->sheet('Summary')->rows();Validate untrusted workbooks before processing#
- Apply file-size, ZIP-entry, compression-ratio, path, macro, external-link, and encryption policies.
- Keep uploaded files outside the public root and use application-generated filenames.
- Limit formulas, source rows, columns, shared strings, and processing time before large imports.
- Run XLSX integrity validation after generating critical reports.
See the security overview and production checklist for complete controls.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue