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

November 2022 Update

Since Rector 0.14, this package is decopuled as standalone extension, as useful for one-time migration only.

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:

<div class="col-12 col-sm-6">
    <a href="">
        <img src="/assets/images/blog/2020/sheets_phpspreadsheet_stats.png" class="img-thumbnail mt-3 mb-3">

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-phpoffice --dev
  1. Create rector.php config
vendor/bin/rector init

# on Windows?
vendor\bin\rector init
  1. Add your set to the rector.php config:
use Rector\PHPOffice\Set\PHPOfficeSetList;
use Rector\Config\RectorConfig;

return function (RectorConfig $rectorConfig): void {
  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!