Reading workbooks
Read an XLSX Workbook
Open an XLSX workbook, inspect its worksheets, choose the right row shape, and process records with a deliberate terminal method.
MnbExcel::read()ReadSession::sheetNames()ReadSession::activeSheetInfo()ReadSession::inspect()ReadSession::sheet()ReadSession::withHeaderRow()ReadSession::withoutHeaderRow()ReadSession::rows()ReadSession::toArray()Open once, inspect the workbook, then choose the worksheet deliberately.
XLSX files can contain several worksheets and an active-sheet setting. Inspect that structure before business code assumes where the data lives.
- 01OpenCreate one read session.
- 02InspectSee sheets and workbook metadata.
- 03SelectChoose the intended worksheet.
- 04ShapeDecide header or positional rows.
- 05ExecuteStream or collect the result.
sheetNames()- Discover worksheet names before selecting one by business meaning.
activeSheetInfo()- Inspect the workbook-defined active worksheet instead of assuming the first tab is primary.
inspect()- Use workbook-level inspection when the application needs structure before row processing starts.
$workbook = MnbExcel::read('orders.xlsx');
$sheetNames = $workbook->sheetNames();
$activeSheet = $workbook->activeSheetInfo();
$inspection = $workbook->inspect();Use named rows for records. Use positional rows for matrices.
Header handling changes the PHP shape your loop receives. Choose it from the document model rather than from habit.
Associative rows survive column reordering more safely.
Use a declared header row when columns have stable business names such as Order ID, Email, Amount, or Status.
sheet()withHeaderRow()rows()$orders = MnbExcel::read('orders.xlsx')
->sheet('Orders')
->withHeaderRow(1);
foreach ($orders->rows() as $row) {
printf("%s — %0.2f\n", $row['Order ID'], $row['Amount']);
}Positional rows preserve coordinate-oriented structure.
Use numeric indexes when column position is itself meaningful and there is no business header contract.
sheet()withoutHeaderRow()rows()$rows = MnbExcel::read('matrix.xlsx')
->sheet(1)
->withoutHeaderRow()
->rows();
foreach ($rows as $row) {
$firstCell = $row[0] ?? null;
}Stream records when you can process them incrementally. Collect only when the dataset belongs in memory.
Both approaches are valid. The important boundary is the type returned by the terminal method and how much row data the application retains.
rows()Generator / iterable rowsIncrementalEach record can be validated, transformed, persisted, or exported as it arrives.
toArray()Native PHP arrayCollectedThe complete result is modest enough that later application logic genuinely benefits from an in-memory array.
Keep the read and the processing loop together.
This makes the application’s one-pass intent visible.
$session = MnbExcel::read('orders.xlsx')
->sheet('Orders')
->withHeaderRow(1);
foreach ($session->rows() as $row) {
processOrder($row);
}After toArray(), work with a normal PHP array.
Session methods are no longer available on the returned array.
$rows = MnbExcel::read('small-reference.xlsx')
->sheet('Countries')
->withHeaderRow(1)
->toArray();
if ($rows === []) {
return;
}
foreach ($rows as $row) {
processCountry($row);
}Move to the specialist guide when sheet rules, terminal choice, or file size becomes the main problem.
This page covers the normal XLSX reading path. The linked guides go deeper without making the basic workflow harder to scan.