Migrate from PHPExcel to PHP 8
Replace legacy PHPExcel installation and workbook patterns with Composer packages, the MnbExcel facade, read sessions, and modern workbook builders.
Updated
PHP 8.1+Full packageMigration
MnbExcel::read()MnbExcel::fromArray()ReadSession::rows()WorkbookBuilder::save()What you will learn
- Replace manual includes with Composer autoloading.
- Translate workbook loading and row iteration to read sessions.
- Translate array exports to a modern workbook builder.
Install the modern package#
composer require mnb/mnb-phpexcel:^2.0Replace workbook loading and worksheet iteration#
$objPHPExcel = PHPExcel_IOFactory::load('orders.xlsx');
$worksheet = $objPHPExcel->getSheetByName('Orders');
foreach ($worksheet->getRowIterator() as $row) {
// Read cell iterators manually.
}use Mnb\PHPExcel\MnbExcel;
$session = MnbExcel::read('orders.xlsx')
->sheet('Orders')
->withHeaderRow(1);
foreach ($session->rows() as $row) {
echo $row['Order ID'] . PHP_EOL;
}Replace workbook creation from arrays#
$excel = new PHPExcel();
$sheet = $excel->getActiveSheet();
$sheet->fromArray($rows, null, 'A1');
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$writer->save('report.xlsx');use Mnb\PHPExcel\MnbExcel;
MnbExcel::fromArray($rows)
->withHeader()
->autoWidth()
->save('report.xlsx');Migration contract#
- Installation
- Composer package and Composer autoloading.
- Reading model
ReadSessionwith chainable configuration and explicit terminal methods.- Writing model
WorkbookBuilderwith chainable workbook configuration andsave().- Return types
- Generators, native arrays, structured arrays, scalar values, and builder objects are documented explicitly.
- Large files
- Use row streaming and chunks instead of materializing a complete workbook object when the job is record-oriented.
- Migration strategy
- Move one workflow at a time and verify generated workbooks in Excel or LibreOffice.
Expected output
Legacy loader → MnbExcel::read()
Worksheet iteration → rows()
fromArray + writer → MnbExcel::fromArray()->save()Preserve regression fixtures so values, formulas, sheet names, formatting, and output files remain verifiable.
Continue the migration#
GUIDEPHPExcel PHP 8 alternative
Review package concepts, common replacements, and migration guidance.
→READINGFluent vs terminal methodsLearn why configuration calls and execution calls have different return types.
→CHECKLISTCommon mistakesAvoid zero-based sheet numbers, array/session confusion, and unsafe upload paths.
→Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue