PhpSpreadsheet 通过引入命名空间和重命名一些类引入了许多破坏性更改。为了帮助您迁移现有项目,编写了一个工具来替换所有对 PHPExcel 类的引用到它们新名称。但是还需要进行手动更改。

自动化工具

RectorPHP 可用于自动迁移您的代码库。假设要迁移的文件位于 src/ 目录下,可以按照如下方式运行迁移:

  1. composer require rector/rector rector/rector-phpoffice phpoffice/phpspreadsheet --dev
  2. # this creates rector.php config
  3. vendor/bin/rector init

rector.php 中设置 PHPOfficeSetList

  1. declare(strict_types=1);
  2. use Rector\Config\RectorConfig;
  3. use Rector\PHPOffice\Set\PHPOfficeSetList;
  4. return static function (RectorConfig $rectorConfig): void {
  5. $rectorConfig->sets([
  6. PHPOfficeSetList::PHPEXCEL_TO_PHPSPREADSHEET
  7. ]);
  8. };

在代码中执行 Rector 代码

vendor/bin/rector process src

有关更多详细信息,请参阅 rector-phpoffice。