How to Migrate From PHPExcel to PHPSpreadsheet with Rector in 30 minutes

PHPExcel is a package for working with Excel files in PHP. The last version was released in 2015, and it was deprecated in 2017. Still, it has over 27 000 daily downloads - that's tons of legacy code.

Do you use it too? Do you want to switch to PHPSpreadsheet? You can do it today.

PHPSpreadsheet is direct follower of PHPExcel with the same maintainers, just with dozens of BC breaks. There is the official migration tutorial that describes step by step 24 different changes that you need to do.

Only 1 of those changes - class renames - is automated with preg_replace(). Regular expressions are not the best thing to use to modify code, rather contrary.


But there are also extra method calls:

-$worksheet->getDefaultStyle();
+$worksheet->getParent()->getDefaultStyle();

Method call renames:

-$worksheet->setSharedStyle($sharedStyle, $range);
+$worksheet->duplicateStyle($sharedStyle, $range);

Argument switch and extra method calls:

-$cell = $worksheet->setCellValue('A1', 'value', true);
+$cell = $worksheet->getCell('A1')->setValue('value');

Method move to another class:

-PHPExcel_Cell::absoluteCoordinate()
+PhpOffice\PhpSpreadsheet\Cell\Coordinate::absoluteCoordinate()

and so on.


Most people use PHPExcel in the past, and didn't touch the code ever since. If it works, why touch it, right?

That why for last 3 years the download rate decreased just very poorly - from ~25 000 daily downlaod to ~20 000 daily downloads:

We need Migration - Fast

We got into a project that used PHPExcel and wanted to switch to PHP 7.4 and Symfony 5. PHP upgrade and framework migration is half of the work. The other half are these small packages, that vendor locks your project to old PHP or another old dependency.

To get rid of old PHPExcel, we prepare a set phpexcel-to-phpspreadsheet to help us.

It took 3 days to make and now has 230 lines of configuration and 17 rules.

How to Migrate?

composer require rector/rector --dev
vendor/bin/rector process src --set phpexcel-to-phpspreadsheet

That's it! Rector just migrated your code from PHPExcel to PHPSpreadsheet.

If you have any issues, look at official migration tutorial or let us know on GitHub.


Happy coding!