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:


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:


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?

  1. Install Rector
composer require rector/rector --dev
  1. Create rector.php config
vendor/bin/rector init
  1. Add your set to the config:
use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::SETS, [
  1. Dry run Rector to see what would be changed
vendor/bin/rector process src --dry-run
  1. Make the changes happen:
vendor/bin/rector process src

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!