用PHPExcel按模板导出数据图片到excel怎么写?

作者站长头像
站长
· 阅读数 4

这个是用PHPExcel按模板导出数据到excel,能成功导出,数据库img字段存放的是图片的网址,导出也是网址,我想把图片导出到excel,应该怎么改,这有个例子,不会改,请教https://blog.csdn.net/zwm_yy/article/details/131316293?spm=10...

<?php 
if (! isset ( $_SESSION )) {
    session_start ();
}
if (! isset ( $_SESSION ['userName'] )) {
    header ( "location:login.php" );
}
$userName = $_SESSION ['userName'];
require_once 'dbconfig.php';
$query = "select * from taizhang ";
$result = mysqli_query($conn,$query);
//数据库img字段存放的是图片的网址,

  include __DIR__ . '/PHPExcel/PHPExcel.php';

  class PHPExcelHelper
  {


    /**
     * 根据模板导出文件,(完美支持:超过26列、公式、自定义多行模板头、列显示顺序)
     * 模板列字段要填写和data数组key对应该上
     * @param $saveFileName 要保存的文件名
     * @param $templateFilePath 模板路径绝对全路径
     * @param $data[] 要导出的数据
     * @return string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public static function exportByTemplate($saveFileName, $data, $templateFilePath)
    {

      $date = date("Y_m_d", time());
      $saveFileName .= ".xls";
      if (!file_exists($templateFilePath)) {
        echo "模板文件不存在!";
        return "模板文件不存在!";
      }

      $objReader = \PHPExcel_IOFactory::load($templateFilePath);
      $sheet = $objReader->getSheet(0);
      $highestRow = $sheet->getHighestRow();
      $highestColumnCode = $sheet->getHighestColumn();

      $colus = []; //列名和列对应的变量名称
      $maxColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumnCode);
      for ($c = 0; $c < $maxColumnIndex; $c++) {
        $colName = \PHPExcel_Cell::stringFromColumnIndex($c);
        $colus[$colName] = trim($sheet->getCell($colName . $highestRow)->getValue());
      }
      foreach ($data as $item) {
        foreach ($colus as $k => $v) {
          if (strpos($v, '=') === 0) {
            $formula = preg_replace("/([A-Z])(\d+)/", '${1}' . $highestRow, $v);
            $sheet->setCellValue($k . $highestRow, $formula);
          } else {
            $sheet->setCellValue($k . $highestRow, $item[$v] ?? "");
          }
        }
        $highestRow++;
      }
      $excel_name = iconv("utf-8", "gb2312", $saveFileName);
      ob_end_clean(); //清除缓冲区,避免乱码
      header('Content-Type: application/vnd.ms-excel');
      header("Content-Disposition: attachment;filename=\"$excel_name\"");
      header('Cache-Control: max-age=0');
      $objWriter = \PHPExcel_IOFactory::createWriter($objReader, 'Excel5');
      $objWriter->save('php://output'); //文件通过浏览器下载
    }
  }
  $templateFilePath = __DIR__ . '/template.xlsx';

  //  require("db333.php");
 // $result = mysqli_query($conn, "select * from gz2021 where a63='$yue' and a1 like '%$name2%' "); //打开你的表
  $data = "";
  $array = array();
  $array1 = array();
  $len = 8;
 // for ($i = 1; $i <= $len; $i++) {
 //   $ar[] = 'a' . $i;
 // }
 $ar[0] = 'pxname';
  $ar[1] = 'neirong';
   $ar[2] = 'className';
    $ar[3] = 'leibie';
     $ar[4] = 'riqi';
      $ar[5] = 'name';
       $ar[6] = 'img';
       $ar[7] = 'img2';


  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    for ($i = 0; $i < $len; $i++) {
      $array1[$ar[$i]] = $row[$ar[$i]];
    // echo   $row[$ar[$i]];
    }
    $array[] = $array1;
  }
  //var_dump($array);
  PHPExcelHelper::exportByTemplate($c1 . '培训台账表', $array, $templateFilePath);

感谢乔治老师的指导,加上下面这段搞定了。

//图片路径必须是本地
$imgPath = $item['img'];
if($imgPath<>""){
    $imgPath  = substr($imgPath,21,strlen($imgPath)-2-20);
 
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($imgPath);
$objDrawing->setHeight(45); 
$objDrawing->setWidth(40); //照片宽度
$objDrawing->setCoordinates("$k$highestRow"); 
$objDrawing->setWorksheet($sheet);
回复
1个回答
avatar
test
2024-06-29
$imageUrl = $item['img'];
$gdImage = imagecreatefromjpeg($imageUrl); 

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Sample image');
$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150); 
$objDrawing->setCoordinates($k . $highestRow); 

$objDrawing->setWorksheet($sheet);
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容