5

I am really mad at PHPSpreadsheet, and why the functions are not named as in PHPExcel, or why in documentation doesn't mention something about "PHPExcel_Worksheet_Drawing", where can I find it in PHPSpreadsheet?.

In PHPExcel I have this:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath(Sys::$_R["images"].'logo_med.png');
$objDrawing->setHeight(110);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

Now, how can I achieve that?

Máxima Alekz
  • 438
  • 7
  • 21
  • 1
    You are comparing at two different libraries, See: The [Migration file](https://github.com/PHPOffice/PhpSpreadsheet/blob/fff363078078753a2092b7e945514642309bccdc/src/PhpSpreadsheet/Helper/Migrator.php#L170) or The [Drawing object](https://github.com/PHPOffice/PhpSpreadsheet/blob/develop/src/PhpSpreadsheet/Worksheet/Drawing.php) file itself or the [Documentation](https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#add-a-drawing-to-a-worksheet) – Will B. Jan 05 '18 at 22:37
  • I have tried to run the Migration exec, but all classnames got wrong – Máxima Alekz Jan 05 '18 at 22:42
  • "class \PHPOffice\Worksheets\Drawing{}" That's a Syntax Error. – Máxima Alekz Jan 05 '18 at 22:43
  • What version of PHP are you using? the `::class` constant was added in PHP 5.5 – Will B. Jan 05 '18 at 22:44
  • PHP FPM 7...... – Máxima Alekz Jan 05 '18 at 22:50
  • We apologise for trying to convert PHPExcel into a modern library fit for 21st century PHP rather than simply restricting ourselves to 20th century coding practises, and for assuming that users were living in the 21st century – Mark Baker Jan 06 '18 at 09:17

1 Answers1

7

Well, the answer for me was find every class to "match" PHPExcel-PHPSpreadsheet..

require_once 'path/to/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet as spreadsheet; // instead PHPExcel
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as xlsx; // Instead PHPExcel_Writer_Excel2007
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing as drawing; // Instead PHPExcel_Worksheet_Drawing
use PhpOffice\PhpSpreadsheet\Style\Alignment as alignment; // Instead PHPExcel_Style_Alignment
use PhpOffice\PhpSpreadsheet\Style\Fill as fill; // Instead PHPExcel_Style_Fill
use PhpOffice\PhpSpreadsheet\Style\Color as color_; //Instead PHPExcel_Style_Color
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup as pagesetup; // Instead PHPExcel_Worksheet_PageSetup
use PhpOffice\PhpSpreadsheet\IOFactory as io_factory; // Instead PHPExcel_IOFactory

Then my code:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath(Sys::$_R["images"].'logo_med.png');
$objDrawing->setHeight(110);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

Is now:

$objPHPExcel = new spreadsheet();
...
$objDrawing = new drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath(Sys::$_R["images"].'logo_med.png');
$objDrawing->setHeight(110);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
...
$objWriter = io_factory::createWriter($objPHPExcel, 'Xlsx');
$objWriter->save("route/to/save/file.xlsx");

The trick was, that have to find function names in every PHPExcel_* class that match with PHPSpreadsheet files, and that's how I achieved this. Take care about https://phpspreadsheet.readthedocs.io/en/develop/topics/migration-from-PHPExcel/#migration-from-phpexcel , that comparing table.

I hope this will be useful.

Máxima Alekz
  • 438
  • 7
  • 21