Thinkphp5 做PHPexcel 图片导出

控制器层:


public function index(){

    if(isset($param['export']) && $param['export'] == 1 ){  // EXCEL 导出

             $list2 = $GoodsProductModel->getExcelList($condition);

             foreach($list2 as $key => $val){

                 $list2[$key]['platform'] = is_platform($list2[$key]['platform']);
                 $list2[$key]['user_commission'] = is_commission($list2[$key]['commission_type'],$list2[$key]['commission'],$list2[$key]['post_price']);

             }

             $header = [
                 'A' => ['post_title', '商品名称'],
                 'B' => ['type_name', '商品分类'],
                 'C' => ['post_price', '商品价格(元)'],
                 'D' => ['user_commission', '商品佣金(元/件)'],
                 'E' => ['platform', '可上平台'],
                 'F' => ['create_time', '上架时间'],
                 'G' => ['thumb', '缩略图']
             ];

             $fileName = '商品明细'.date('Y-m-d').rand(0,100);
             $this->excel($header, $list2, $fileName);       // Excel 导出,在公共方法中
         }

}

// Excel 导出
function excel($header,$list,$fileName){

        // 引入 Excel 表格插件
        import('PHPExcel.Classes.PHPExcel');
        import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
        $PHPExcel = new \PHPExcel();
        $PHPSheet = $PHPExcel->getActiveSheet();

        // 设置表格宽度
        $PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(35);
        $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
        $PHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);

        // 设置表格高度
        $PHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);     //第一行行高

        // 第一行加粗
        $PHPExcel->getActiveSheet()->getStyle('A1:G1')->getFont()->setBold(true);

        // 设置字体
        $PHPExcel->getDefaultStyle()->getFont()->setName('微软雅黑');             //字体

        foreach ($list as $k => $v) {
            foreach ($header as $key => $value) {           // $key 最大值为6
                if ($k == 0) {
                    $PHPSheet->setCellValue($key.'1', end($value));
                }
                $objDrawing = new \PHPExcel_Worksheet_Drawing();

                $i = $k + 2;
                $PHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(45);
                if($key == 'G'){
                    $objDrawing->setPath('./public/uploads/image/' . $v['thumb']);//这里拼接 . 是因为要在根目录下获取
                    // 设置宽度高度
                    $objDrawing->setHeight(50); //照片高度
                    $objDrawing->setWidth(50);  //照片宽度
                    /* 设置图片要插入的单元格 */
                    $objDrawing->setCoordinates($key.$i, $v[reset($value)]);
                    // 图片偏移距离
                    $objDrawing->setOffsetX(5);
                    $objDrawing->setOffsetY(5);
                    $objDrawing->setWorksheet($PHPExcel->getActiveSheet());
                }else{
                    $PHPSheet->setCellValue($key.$i, $v[reset($value)]);
                }

            }
        }

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$fileName.'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $PHPWriter->save("php://output");

    }

 

上一篇:剑指offer-16-合并两个排序的链表


下一篇:Day53(泛型方法,泛型在继承上的体现,通配符的使用,有限制的通配符)