phpexcel导出图片

phpexcel的使用可参考我之前写的教程 Laravel和TP导出数据Excel

# 导入的类
use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Worksheet_Drawing;

/**
  * 导出Excel方法
  */
 public function export()
{
     
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        # 整理数据
        $this->model
        ->where($where)
        ->order($sort, $order);
        $list = $this->model->select();
        $list = collection($list)->toArray();
        $count = count($list); // 总条数
        # 填充表头数据
        $head = ['身份证号','身份证图片','联系号码'];
        # 获取字母列表
        $letterList = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        # 找到当前脚本所在路径
        $path = dirname(__FILE__);
        # 实例化phpexcel
        $PHPExcel = new \PHPExcel();
        $PHPSheet = $PHPExcel->getActiveSheet();
        # 设置表内部名称
        $PHPSheet->setTitle("demo");
        # 设置表头数据
        foreach ($head as $kk => $vv) {
     
            $PHPSheet->setCellValue($letterList[$kk]."1", $vv);
        }
        # 设置宽度
        $PHPSheet->getColumnDimension('B')->setWidth(11);
        # 设置高度
        for ($i = 2; $i <= $count+1 ; $i++) {
      
            $PHPSheet->getRowDimension($i)->setRowHeight(60);
        }
        $num = 2; // 从第二行开始插入
        # 数据代入
        foreach ($list as $k => $v) {
     
            $PHPSheet->setCellValue("A" . $num, "\t" . $v['idno'] . "\t");
            # 输出图片
            if ($v['front_photo']){
     
                $objDrawing = new \PHPExcel_Worksheet_Drawing(); //必须每次重新实例化
                $objDrawing->setPath('.'.$v['front_photo']); //这里是相对路径
                $objDrawing->setWidth(80); // 设置图片宽度
                $objDrawing->setCoordinates('B'.($num-1)); // 图片是从1开始的 不会直接在表头
                # 图片偏移距离
                # $objDrawing->setOffsetX(12);
                $objDrawing->setOffsetY(80);
                $objDrawing->setWorksheet($PHPExcel->getActiveSheet());
            }
            $PHPSheet->setCellValue("C" . $num, $v['mobile']);
            $num++;
        }
        # 文档名称
        $filename = date('YmdHis');
        # 创建生成的格式
        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        # 代入文档名称
        header("Content-Disposition: attachment;filename=$filename.xlsx");
        header("Content-Type: text/html;charset=utf-8");
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        # 生成文件
        $PHPWriter->save("php://output");
        exit();
    }


你可能感兴趣的:(后端,excel,php)