Skip to content
MNB PHPExcelDeveloper Guide
AI

XLSX library

Reading XLSX Workbooks

Use read sessions for sheets, headers, projection, ranges, formulas, dates, images, and structured output.

Level
Intermediate
Reading time
10 min
Package
mnb/mnb-phpexcel-xlsx
Updated
sheetNames()activeSheet()sheetIfExists()hasRows()autoDetectHeader()projectColumns()cellDetails()chunks()

Inspect sheets and choose one#

PHP
$session = Xlsx::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#

PHP
$rows = Xlsx::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#

PHP
$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#

PHP
$session = Xlsx::read('orders.xlsx')
    ->sheet('Orders')
    ->withHeaderRow(1)
    ->onRowError('collect');

foreach ($session->chunks(1000) as $chunk) {
    processOrders($chunk);
}

$errors = $session->rowErrors();

Use the remaining XLSX library pages in this sidebar for standalone-package reading, writing, inspection, and security workflows.