Reading XLSX Workbooks
Use read sessions for sheets, headers, projection, ranges, formulas, dates, images, and structured output.
Updated
sheetNames()activeSheet()sheetIfExists()hasRows()autoDetectHeader()projectColumns()cellDetails()chunks()What you will learn
- Select worksheets by index or name and normalize headers.
- Reduce work through row ranges and source-level column projection.
- Choose direct cell, row generator, chunk, and structured-output APIs.
Inspect sheets and choose one#
$session = MnbExcel::read('report.xlsx');
print_r($session->sheetNames());
print_r($session->activeSheetInfo());
$rows = ($session->sheetIfExists('Sales') ?? $session->activeSheet())
->autoDetectHeader(sampleRows: 25, minimumConfidence: 0.45)
->rows();Project and stream only what you need#
$rows = MnbExcel::read('orders.xlsx')
->sheet('Orders')
->withHeaderRow(1)
->projectColumns(['Order ID', 'Customer', 'Amount'])
->range(startRow: 1, endRow: 100000)
->streaming()
->rows();Projection is applied before the application retains discarded values. For large workbooks, combine projection with the large-file preflight and streaming APIs.
Use direct access for lookup workbooks#
$total = Xlsx::cell('finance.xlsx', 'D12', 'Summary');
$matrix = Xlsx::rangeValues('finance.xlsx', 'A1:F20', 'Summary');
$details = Xlsx::cellDetails('finance.xlsx', 'B7', 'Review');
print_r([
'formula' => $details->formula,
'style' => $details->style,
'comment' => $details->comment,
'hyperlink' => $details->hyperlink,
]);Process batches and preserve failures#
$session = MnbExcel::read('orders.xlsx')
->sheet('Orders')
->withHeaderRow(1)
->onRowError('collect');
foreach ($session->chunks(1000) as $chunk) {
processOrders($chunk);
}
$errors = $session->rowErrors();Continue with the dedicated guides for formulas and dates, images and metadata, and large-file streaming.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue