简体   繁体   中英

phpexcel export to excel with image

I have a table in my Database containing the data of many items with the name of its image.

From that table I want to display each item data with its image. This is the code I use to display it in PHP:

<table>
<thead>
  <tr>
    <th style="border-width:medium;" rowspan="2">CODE</th>
    <th style="border:medium;" rowspan="2">CLR</th>
    <th style="border-width:medium;" rowspan="2">IMAGE</th>
    <th style="border-width:medium;" rowspan="2">DESCRIPTION</th>
    <th style="border-width:medium;" rowspan="2">QTY.</th>
    <th style="border-width:medium;" rowspan="2">PRICE</th>
  </tr>
</thead>
<tbody>
<?php foreach($list as $data){?>
  <tr>
    <td><?=$data->code_caption?></td>
    <td style="text-align:center"><?=$data->color?></td>
    <td style="width:135px; height:135px; text-align:center;">
    <img src="<?=base_url().'media/img/gallery/items/'.$data->image?>TH.jpg" style="margin:5px; text-align:center; vertical-align:middle;" />
    </td>
    <td><?=$data->desc?></td>
    <td style="text-align:center"><?=$data->qty?></td>
    <td style="text-align:right"><?=$data->price?></td>
  </tr>
<?php }?>
</tbody>
</table>

I want to export the data to an Excel formatted file so it looks like what I display on my PHP page.

Can it be done?

I have tried to use the Excel header but the image becomes a link, it's not the complete image being embedded.

And I also saw the PHPexcel library, but I cannot find a way to write the loop with the image.

Any solution for my problem will be very helpful.

Thanks for Helping me I have found the answer for my own question.

    <?php

    $rowNumber = 12;
    foreach($list as $data){
$this->excel->getActiveSheet()->setCellValue('A'.$rowNumber, $data->code_caption)
->setCellValue('B'.$rowNumber, $data->color);

if(file_exists('./media/img/gallery/items/'.$data->image.'TH.jpg'))
{
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setPath('./media/img/gallery/items/'.$data->image.'TH.jpg');
    $objDrawing->setCoordinates('C'.$rowNumber);
    $objDrawing->setWorksheet($this->excel->getActiveSheet());
    $this->excel->getActiveSheet()->getRowDimension($rowNumber)->setRowHeight(120);
}
else
{
    $this->excel->getActiveSheet()->setCellValue('C'.$rowNumber, '');
}

$this->excel->getActiveSheet()->setCellValue('D'.$rowNumber, $data->desc);
$this->excel->getActiveSheet()->setCellValue('E'.$rowNumber, $data->$d_met);
$this->excel->getActiveSheet()->setCellValue('F'.$rowNumber, $data->$w_met);
$this->excel->getActiveSheet()->setCellValue('G'.$rowNumber, $data->$h_met);
$this->excel->getActiveSheet()->setCellValue('H'.$rowNumber, $data->qty);
$this->excel->getActiveSheet()->setCellValue('I'.$rowNumber,$data->$cur_dat);           
$this->excel->getActiveSheet()->setCellValue('J'.$rowNumber,$data->$total_price);
$rowNumber++;
    }?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM