You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

migration-from-PHPExcel.md 11 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. # Migration from PHPExcel
  2. PhpSpreadsheet introduced many breaking changes by introducing
  3. namespaces and renaming some classes. To help you migrate existing
  4. project, a tool was written to replace all references to PHPExcel
  5. classes to their new names. But there are also manual changes that
  6. need to be done.
  7. ## Automated tool
  8. The tool is included in PhpSpreadsheet. It scans recursively all files
  9. and directories, starting from the current directory. Assuming it was
  10. installed with composer, it can be run like so:
  11. ``` sh
  12. cd /project/to/migrate/src
  13. php /project/to/migrate/vendor/phpoffice/phpspreadsheet/bin/migrate-from-phpexcel
  14. ```
  15. **Important** The tool will irreversibly modify your sources, be sure to
  16. backup everything, and double check the result before committing.
  17. ## Manual changes
  18. In addition to automated changes, a few things need to be migrated manually.
  19. ### Renamed readers and writers
  20. When using `IOFactory::createReader()`, `IOFactory::createWriter()` and
  21. `IOFactory::identify()`, the reader/writer short names are used. Those were
  22. changed, along as their corresponding class, to remove ambiguity:
  23. Before | After
  24. -----------------|---------
  25. `'CSV'` | `'Csv'`
  26. `'Excel2003XML'` | `'Xml'`
  27. `'Excel2007'` | `'Xlsx'`
  28. `'Excel5'` | `'Xls'`
  29. `'Gnumeric'` | `'Gnumeric'`
  30. `'HTML'` | `'Html'`
  31. `'OOCalc'` | `'Ods'`
  32. `'OpenDocument'` | `'Ods'`
  33. `'PDF'` | `'Pdf'`
  34. `'SYLK'` | `'Slk'`
  35. ### Simplified IOFactory
  36. The following methods :
  37. - `PHPExcel_IOFactory::getSearchLocations()`
  38. - `PHPExcel_IOFactory::setSearchLocations()`
  39. - `PHPExcel_IOFactory::addSearchLocation()`
  40. were replaced by `IOFactory::registerReader()` and `IOFactory::registerWriter()`. That means
  41. IOFactory now relies on classes autoloading.
  42. Before:
  43. ```php
  44. \PHPExcel_IOFactory::addSearchLocation($type, $location, $classname);
  45. ```
  46. After:
  47. ```php
  48. \PhpOffice\PhpSpreadsheet\IOFactory::registerReader($type, $classname);
  49. ```
  50. ### Removed deprecated things
  51. #### Worksheet::duplicateStyleArray()
  52. ``` php
  53. // Before
  54. $worksheet->duplicateStyleArray($styles, $range, $advanced);
  55. // After
  56. $worksheet->getStyle($range)->applyFromArray($styles, $advanced);
  57. ```
  58. #### DataType::dataTypeForValue()
  59. ``` php
  60. // Before
  61. DataType::dataTypeForValue($value);
  62. // After
  63. DefaultValueBinder::dataTypeForValue($value);
  64. ```
  65. #### Conditional::getCondition()
  66. ``` php
  67. // Before
  68. $conditional->getCondition();
  69. // After
  70. $conditional->getConditions()[0];
  71. ```
  72. #### Conditional::setCondition()
  73. ``` php
  74. // Before
  75. $conditional->setCondition($value);
  76. // After
  77. $conditional->setConditions($value);
  78. ```
  79. #### Worksheet::getDefaultStyle()
  80. ``` php
  81. // Before
  82. $worksheet->getDefaultStyle();
  83. // After
  84. $worksheet->getParent()->getDefaultStyle();
  85. ```
  86. #### Worksheet::setDefaultStyle()
  87. ``` php
  88. // Before
  89. $worksheet->setDefaultStyle($value);
  90. // After
  91. $worksheet->getParent()->getDefaultStyle()->applyFromArray([
  92. 'font' => [
  93. 'name' => $pValue->getFont()->getName(),
  94. 'size' => $pValue->getFont()->getSize(),
  95. ],
  96. ]);
  97. ```
  98. #### Worksheet::setSharedStyle()
  99. ``` php
  100. // Before
  101. $worksheet->setSharedStyle($sharedStyle, $range);
  102. // After
  103. $worksheet->duplicateStyle($sharedStyle, $range);
  104. ```
  105. #### Worksheet::getSelectedCell()
  106. ``` php
  107. // Before
  108. $worksheet->getSelectedCell();
  109. // After
  110. $worksheet->getSelectedCells();
  111. ```
  112. #### Writer\Xls::setTempDir()
  113. ``` php
  114. // Before
  115. $writer->setTempDir();
  116. // After, there is no way to set temporary storage directory anymore
  117. ```
  118. ### Autoloader
  119. The class `PHPExcel_Autoloader` was removed entirely and is replaced by composer
  120. autoloading mechanism.
  121. ### Writing PDF
  122. PDF libraries must be installed via composer. And the following methods were removed
  123. and are replaced by `IOFactory::registerWriter()` instead:
  124. - `PHPExcel_Settings::getPdfRenderer()`
  125. - `PHPExcel_Settings::setPdfRenderer()`
  126. - `PHPExcel_Settings::getPdfRendererName()`
  127. - `PHPExcel_Settings::setPdfRendererName()`
  128. Before:
  129. ```php
  130. \PHPExcel_Settings::setPdfRendererName(PHPExcel_Settings::PDF_RENDERER_MPDF);
  131. \PHPExcel_Settings::setPdfRenderer($somePath);
  132. $writer = \PHPExcel_IOFactory::createWriter($spreadsheet, 'PDF');
  133. ```
  134. After:
  135. ```php
  136. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
  137. // Or alternatively
  138. \PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
  139. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf');
  140. // Or alternatively
  141. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
  142. ```
  143. ### Rendering charts
  144. When rendering charts for HTML or PDF outputs, the process was also simplified. And while
  145. JpGraph support is still available, it is unfortunately not up to date for latest PHP versions
  146. and it will generate various warnings.
  147. If you rely on this feature, please consider
  148. contributing either patches to JpGraph or another `IRenderer` implementation (a good
  149. candidate might be [CpChart](https://github.com/szymach/c-pchart)).
  150. Before:
  151. ```php
  152. $rendererName = \PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
  153. $rendererLibrary = 'jpgraph3.5.0b1/src/';
  154. $rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
  155. \PHPExcel_Settings::setChartRenderer($rendererName, $rendererLibraryPath);
  156. ```
  157. After:
  158. Require the dependency via composer:
  159. ```sh
  160. composer require jpgraph/jpgraph
  161. ```
  162. And then:
  163. ```php
  164. Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
  165. ```
  166. ### PclZip and ZipArchive
  167. Support for PclZip were dropped in favor of the more complete and modern
  168. [PHP extension ZipArchive](https://php.net/manual/en/book.zip.php).
  169. So the following were removed:
  170. - `PclZip`
  171. - `PHPExcel_Settings::setZipClass()`
  172. - `PHPExcel_Settings::getZipClass()`
  173. - `PHPExcel_Shared_ZipArchive`
  174. - `PHPExcel_Shared_ZipStreamWrapper`
  175. ### Cell caching
  176. Cell caching was heavily refactored to leverage
  177. [PSR-16](https://www.php-fig.org/psr/psr-16/). That means most classes
  178. related to that feature were removed:
  179. - `PHPExcel_CachedObjectStorage_APC`
  180. - `PHPExcel_CachedObjectStorage_DiscISAM`
  181. - `PHPExcel_CachedObjectStorage_ICache`
  182. - `PHPExcel_CachedObjectStorage_Igbinary`
  183. - `PHPExcel_CachedObjectStorage_Memcache`
  184. - `PHPExcel_CachedObjectStorage_Memory`
  185. - `PHPExcel_CachedObjectStorage_MemoryGZip`
  186. - `PHPExcel_CachedObjectStorage_MemorySerialized`
  187. - `PHPExcel_CachedObjectStorage_PHPTemp`
  188. - `PHPExcel_CachedObjectStorage_SQLite`
  189. - `PHPExcel_CachedObjectStorage_SQLite3`
  190. - `PHPExcel_CachedObjectStorage_Wincache`
  191. In addition to that, `\PhpOffice\PhpSpreadsheet::getCellCollection()` was renamed
  192. to `\PhpOffice\PhpSpreadsheet::getCoordinates()` and
  193. `\PhpOffice\PhpSpreadsheet::getCellCacheController()` to
  194. `\PhpOffice\PhpSpreadsheet::getCellCollection()` for clarity.
  195. Refer to [the new documentation](./memory_saving.md) to see how to migrate.
  196. ### Dropped conditionally returned cell
  197. For all the following methods, it is no more possible to change the type of
  198. returned value. It always return the Worksheet and never the Cell or Rule:
  199. - Worksheet::setCellValue()
  200. - Worksheet::setCellValueByColumnAndRow()
  201. - Worksheet::setCellValueExplicit()
  202. - Worksheet::setCellValueExplicitByColumnAndRow()
  203. - Worksheet::addRule()
  204. Migration would be similar to:
  205. ``` php
  206. // Before
  207. $cell = $worksheet->setCellValue('A1', 'value', true);
  208. // After
  209. $cell = $worksheet->getCell('A1')->setValue('value');
  210. ```
  211. ### Standardized keys for styling
  212. Array keys used for styling have been standardized for a more coherent experience.
  213. It now uses the same wording and casing as the getter and setter:
  214. ```php
  215. // Before
  216. $style = [
  217. 'numberformat' => [
  218. 'code' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE,
  219. ],
  220. 'font' => [
  221. 'strike' => true,
  222. 'superScript' => true,
  223. 'subScript' => true,
  224. ],
  225. 'alignment' => [
  226. 'rotation' => 90,
  227. 'readorder' => Alignment::READORDER_RTL,
  228. 'wrap' => true,
  229. ],
  230. 'borders' => [
  231. 'diagonaldirection' => Borders::DIAGONAL_BOTH,
  232. 'allborders' => [
  233. 'style' => Border::BORDER_THIN,
  234. ],
  235. ],
  236. 'fill' => [
  237. 'type' => Fill::FILL_GRADIENT_LINEAR,
  238. 'startcolor' => [
  239. 'argb' => 'FFA0A0A0',
  240. ],
  241. 'endcolor' => [
  242. 'argb' => 'FFFFFFFF',
  243. ],
  244. ],
  245. ];
  246. // After
  247. $style = [
  248. 'numberFormat' => [
  249. 'formatCode' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE,
  250. ],
  251. 'font' => [
  252. 'strikethrough' => true,
  253. 'superscript' => true,
  254. 'subscript' => true,
  255. ],
  256. 'alignment' => [
  257. 'textRotation' => 90,
  258. 'readOrder' => Alignment::READORDER_RTL,
  259. 'wrapText' => true,
  260. ],
  261. 'borders' => [
  262. 'diagonalDirection' => Borders::DIAGONAL_BOTH,
  263. 'allBorders' => [
  264. 'borderStyle' => Border::BORDER_THIN,
  265. ],
  266. ],
  267. 'fill' => [
  268. 'fillType' => Fill::FILL_GRADIENT_LINEAR,
  269. 'startColor' => [
  270. 'argb' => 'FFA0A0A0',
  271. ],
  272. 'endColor' => [
  273. 'argb' => 'FFFFFFFF',
  274. ],
  275. ],
  276. ];
  277. ```
  278. ### Dedicated class to manipulate coordinates
  279. Methods to manipulate coordinates that used to exists in `PHPExcel_Cell` were extracted
  280. to a dedicated new class `\PhpOffice\PhpSpreadsheet\Cell\Coordinate`. The methods are:
  281. - `absoluteCoordinate()`
  282. - `absoluteReference()`
  283. - `buildRange()`
  284. - `columnIndexFromString()`
  285. - `coordinateFromString()`
  286. - `extractAllCellReferencesInRange()`
  287. - `getRangeBoundaries()`
  288. - `mergeRangesInCollection()`
  289. - `rangeBoundaries()`
  290. - `rangeDimension()`
  291. - `splitRange()`
  292. - `stringFromColumnIndex()`
  293. ### Column index based on 1
  294. Column indexes are now based on 1. So column `A` is the index `1`. This is consistent
  295. with rows starting at 1 and Excel function `COLUMN()` that returns `1` for column `A`.
  296. So the code must be adapted with something like:
  297. ```php
  298. // Before
  299. $cell = $worksheet->getCellByColumnAndRow($column, $row);
  300. for ($column = 0; $column < $max; $column++) {
  301. $worksheet->setCellValueByColumnAndRow($column, $row, 'value ' . $column);
  302. }
  303. // After
  304. $cell = $worksheet->getCellByColumnAndRow($column + 1, $row);
  305. for ($column = 1; $column <= $max; $column++) {
  306. $worksheet->setCellValueByColumnAndRow($column, $row, 'value ' . $column);
  307. }
  308. ```
  309. All the following methods are affected:
  310. - `PHPExcel_Worksheet::cellExistsByColumnAndRow()`
  311. - `PHPExcel_Worksheet::freezePaneByColumnAndRow()`
  312. - `PHPExcel_Worksheet::getCellByColumnAndRow()`
  313. - `PHPExcel_Worksheet::getColumnDimensionByColumn()`
  314. - `PHPExcel_Worksheet::getCommentByColumnAndRow()`
  315. - `PHPExcel_Worksheet::getStyleByColumnAndRow()`
  316. - `PHPExcel_Worksheet::insertNewColumnBeforeByIndex()`
  317. - `PHPExcel_Worksheet::mergeCellsByColumnAndRow()`
  318. - `PHPExcel_Worksheet::protectCellsByColumnAndRow()`
  319. - `PHPExcel_Worksheet::removeColumnByIndex()`
  320. - `PHPExcel_Worksheet::setAutoFilterByColumnAndRow()`
  321. - `PHPExcel_Worksheet::setBreakByColumnAndRow()`
  322. - `PHPExcel_Worksheet::setCellValueByColumnAndRow()`
  323. - `PHPExcel_Worksheet::setCellValueExplicitByColumnAndRow()`
  324. - `PHPExcel_Worksheet::setSelectedCellByColumnAndRow()`
  325. - `PHPExcel_Worksheet::stringFromColumnIndex()`
  326. - `PHPExcel_Worksheet::unmergeCellsByColumnAndRow()`
  327. - `PHPExcel_Worksheet::unprotectCellsByColumnAndRow()`
  328. - `PHPExcel_Worksheet_PageSetup::addPrintAreaByColumnAndRow()`
  329. - `PHPExcel_Worksheet_PageSetup::setPrintAreaByColumnAndRow()`
  330. ### Removed default values
  331. Default values for many methods were removed when it did not make sense. Typically,
  332. setter methods should not have default values. For a complete list of methods and
  333. their original default values, see [that commit](https://github.com/PHPOffice/PhpSpreadsheet/commit/033a4bdad56340795a5bf7ec3c8a2fde005cda24).