PHPExcel PHP 8 Alternative and Migration Guide
Move historical PHPExcel-style code to a modern PHP 8.1+ spreadsheet API with Composer, native XLSX support and practical migration examples.
Updated
MnbExcel::read()ReadSession::withHeaderRow()ReadSession::toArray()MnbExcel::fromArray()WorkbookBuilder::save()What you will learn
- Install a modern PHP 8.1+ spreadsheet package with Composer.
- Translate common workbook-reading and report-writing patterns to MNB PHPExcel.
- Keep migration code explicit about sheets, headers, row types, formulas and unsupported legacy features.
Install the modern PHP 8 package#
composer require mnb/mnb-phpexcel:^2.0composer require mnb/mnb-phpexcel-xlsx:^2.0The full package provides the MnbExcel facade and all documented modules. The individual XLSX package provides Mnb\PHPExcel\Format\Xlsx for focused XLSX-only projects.
Map familiar workbook tasks to the current API#
| Migration task | MNB PHPExcel API | Recommended behavior |
|---|---|---|
| Open an XLSX workbook | MnbExcel::read($path) | Inspect the file and worksheet names before collecting rows. |
| Select a worksheet | sheet(1) or sheet("Orders") | Worksheet numbers are 1-based; prefer names for business imports. |
| Use the first row as headers | withHeaderRow(1) | Keep the header-row number explicit in production code. |
| Read all rows | toArray() | Use for bounded files; stream with rows() or largeRead() for large workbooks. |
| Read one cell | cell("B2") | Use direct cell access when a workbook acts like a configuration document. |
| Create an XLSX report | MnbExcel::fromArray($rows)->save($path) | Use the workbook builder for styles, charts, validation and protection. |
Migrate a common read workflow#
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Mnb\PHPExcel\MnbExcel;
use Mnb\PHPExcel\Support\MnbExcelException;
try {
$workbook = MnbExcel::read(__DIR__ . '/orders.xlsx');
$sheet = $workbook->sheetIfExists('Orders')
?? $workbook->activeSheet();
$rows = $sheet
->withHeaderRow(1)
->withOptions(['skip_empty_rows' => true])
->toArray();
foreach ($rows as $row) {
echo ($row['Order ID'] ?? '') . PHP_EOL;
}
} catch (MnbExcelException $exception) {
print_r($exception->toErrorArray(debug: true));
}Migrate a common write workflow#
<?php
use Mnb\PHPExcel\MnbExcel;
$rows = [
['Order ID' => 1001, 'Customer' => 'Acme', 'Amount' => 249.95],
['Order ID' => 1002, 'Customer' => 'Northwind', 'Amount' => 180.00],
];
MnbExcel::fromArray($rows)
->withHeader()
->freezeHeader()
->autoFilter()
->autoWidth()
->save(__DIR__ . '/orders-report.xlsx');Review compatibility before replacing production code#
- Test representative workbooks from Microsoft Excel and LibreOffice.
- Verify formulas, dates, number formats, merged ranges and protected files explicitly.
- Use native XLSX for modern workbooks and the native BIFF8 XLS package only for required legacy files.
- Run streaming tests with the largest real workbook your application accepts.
- Keep unsupported legacy features visible in migration notes rather than silently dropping them.
Continue with the PHP Excel reader guide, PHP Excel writer guide, and native PHP XLSX library overview.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue