N
N
NinaRudz2018-09-12 11:41:03
PHP
NinaRudz, 2018-09-12 11:41:03

PhpSpreadsheet: images are added to excel but not showing in it - what's wrong?

I generate .xlsx for download using PhpSpreadsheet
Problem with images. They are added to Excel (judging by the significantly increasing file weight), but they are not shown in Excel itself.
Someone can suggest something?
The code for adding an image to Excel is below. Full code regarding PhpSpreadsheet will be added as needed. Too bad you can't attach files here.

$arLetters = range("A","Z"); // заранее неизвестно количество столбцов таблицы

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); 
$Excel_writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);

$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();

foreach($fields_names as  $key => $fields_name):  // это первая строка, заголовки
$activeSheet->setCellValue( $arLetters[$key] .  '1' , $fields_name);  //  это первая строка, заголовки
endforeach; //  это первая строка, заголовки

$keyRow = 1;

// картинки добавляются в первый столбец таблицы - А2, А3, А4 и т.д.
foreach ($arResult as $ORDER)
{
                $keyRow +=1;
                if($ORDER['photo_small']):
                              $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                              $drawing->setName('Image'  . $keyRow);
                              $drawing->setDescription('Image');
                              $drawing->setHeight(80);
                              $drawing->setCoordinates( 'A' .  $keyRow);
                              $drawing->setPath($ORDER['photo_small']);
                              $drawing->setWorksheet($activeSheet);

                endif;

// код заполнения остальных ячеек

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Smilik, 2018-09-16
@sagechild

If you look at the docks , then your code matches them. Therefore, the first thing to check is the correct path to the image. The second is to look at the logs, what warnings, notes come out for this script, maybe they will lead to an error.

G
ghost_d0gg, 2018-10-25
@ghost_d0gg

Your chain order is wrong. I tormented myself for several hours.
Try like this

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setPath($ORDER['photo_small']);
$drawing->setName('Image'  . $keyRow);
$drawing->setDescription('Image');
$drawing->setHeight(80);
$drawing->setCoordinates( 'A' .  $keyRow);
$drawing->setWorksheet($activeSheet);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question