I
I
ImPuuLsE2015-10-10 23:31:18
Yii
ImPuuLsE, 2015-10-10 23:31:18

How to insert image into excel using phpexcel library?

Hello! using https://packagist.org/packages/phpoffice/phpexcel

$model = Product::find()->published()->all();
        $objPHPExcel = new \PHPExcel();
        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Фото')
            ->setCellValue('B1', 'Название')
            ->setCellValue('C1', 'Опт 1')
            ->setCellValue('D1', 'Опт 2')
            ->setCellValue('E1', 'Опт 3')
            ->setCellValue('F1', 'Опт 4')
            ->setCellValue('G1', 'Артикул');
        $i = 1;
        foreach($model as $product){
            $i++;
                $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('B'.$i.'', $product->title)
                ->setCellValue('C'.$i.'', $product->opt_price1)
                ->setCellValue('D'.$i.'', $product->opt_price2)
                ->setCellValue('E'.$i.'', $product->opt_price3)
                ->setCellValue('F'.$i.'', $product->price)
                ->setCellValue('G'.$i.'', $product->id);

                if (file_exists(Yii::getAlias('@webroot') . $product->images[0]->getUrl())) {
                        // Add a drawing to the worksheetecho date('H:i:s') . " Add a drawing to the worksheet\n";
                        $objDrawing = new \PHPExcel_Worksheet_Drawing();
                        $objDrawing->setName('Sample image');
                        $objDrawing->setDescription('Sample image');
                        $objDrawing->setPath(Yii::getAlias('@webroot').$product->images[0]->getUrl());
                        $objDrawing->setHeight(120);
                        $objDrawing->setCoordinates('A'.$i.'');
                        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
                }
        }
        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('Цены на все товары');


        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="pricelist.xls"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;

But the picture is not inserted... What am I doing wrong?
If you enter the wrong path here
$objDrawing->setPath(Yii::getAlias('@webroot').$product->images[0]->getUrl());

then he swears, but everything goes smoothly for me, so I conclude that something is going wrong next ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav Alexandrov, 2016-08-08
@alexyarik

Bitrix example:
$sPicPath = $arElement["PREVIEW_PICTURE"]["SRC"];
$objDrawing->setPath($_SERVER["DOCUMENT_ROOT"].$sPicPath);
the problem is in the paths, if you enable error logging, you would be shown an error that the file does not exist on the specified path or something like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question