Read Excel Files Safely
Open an Excel workbook, inspect it before reading, select a worksheet safely, stream or collect records, and understand every returned value.
Updated
PHP 8.1+Full packageIndividual XLSXStreaming
MnbExcel::read()Xlsx::read()ReadSession::inspect()ReadSession::sheetIfExists()ReadSession::activeSheet()ReadSession::withHeaderRow()ReadSession::rows()What you will learn
- Inspect the workbook before reading business records.
- Select a named sheet with an intentional active-sheet fallback.
- Choose streaming or array collection based on the job.
Install the correct package#
composer require mnb/mnb-phpexcel:^2.0composer require mnb/mnb-phpexcel-xlsx:^2.0The full package exposes MnbExcel. The individual package exposes Xlsx. Both create the same read-session API after the workbook is opened.
Open, inspect, select, and stream#
use Mnb\PHPExcel\MnbExcel;
$path = __DIR__ . '/storage/imports/orders.xlsx';
$workbook = MnbExcel::read($path);use Mnb\PHPExcel\Format\Xlsx;
$path = __DIR__ . '/storage/imports/orders.xlsx';
$workbook = Xlsx::read($path);$inspection = $workbook->inspect();
$sheetNames = $workbook->sheetNames();
if ($sheetNames === []) {
throw new RuntimeException('The workbook has no readable worksheets.');
}
$sheet = $workbook->sheetIfExists('Orders')
?? $workbook->activeSheet();
$session = $sheet
->withHeaderRow(1)
->withOptions([
'skip_empty_rows' => true,
'max_rows' => 5000,
])
->assertHasRows();
foreach ($session->rows() as $row) {
echo $row['Order ID'] . ' — ' . $row['Customer'] . PHP_EOL;
}Developer contract#
- Returns from read()
ReadSession- Returns from rows()
Generator<int, array<string, mixed>>- Reads immediately
read()creates a session;rows()begins row consumption.- Memory behavior
- Rows are yielded one at a time when the native streaming path is available.
- Empty check
hasRows(),isEmpty(), orassertHasRows()on the session.- Errors
MnbExcelExceptionfor library failures; application validation may throwRuntimeException.
Expected output
ORD-1001 — Arjun Stores
ORD-1002 — Northwind RetailThe header row is not yielded as a business record.
Choose how to consume the data#
| Need | Use | Return type |
|---|---|---|
| Process rows with low memory | rows() | Generator |
| Collect all returned rows | toArray() | array |
| Read only the first data row | first() | ?array |
| Check for data without collecting it | hasRows() | bool |
| Get detailed source and skipped-row counts | toStructuredSheetArray() | array |
| Read a fixed XLSX coordinate | cell("B2") | mixed |
Common mistake: treating an array like a session#
$rows = $session->toArray();
$rows->isEmpty();$rows = $session->toArray();
if ($rows === []) {
echo 'No rows found.';
}if ($session->isEmpty()) {
echo 'No rows found.';
}Continue the workflow#
DEEP GUIDERead Excel step by step
Continue through metadata, sheet names, rows, columns, array indexes, cells, and structured counts.
→METHODSChoose a reading methodCompare rows(), toArray(), first(), countRows(), direct cells, and structured output.
→CONCEPTFluent vs terminal methodsUnderstand which calls keep building a session and which calls execute reading.
→Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue