PHPExce 表格导出带图片

首先把PHPExcel的压缩包解压,将class文件拿出 放在 tp5 目录下 vendor ,重命名 PHPExcel;如图所示:
PHPExce 表格导出带图片_第1张图片
vender->PHPExcel->Classes->…

html代码

<div>
    <form action="{:url('admin/upimg/excelphone')}" method="post" enctype="multipart/form-data">
        <input type="submit" value="导出excel" class="layui-btn layui-btn-sm">
    form>
div>

控制器代码

    public function excelphone(){
        $res=Db::table('eb_brand')->select();
        vendor("PHPExcel.Classes.PHPExcel");
        $resultPHPExcel=new \PHPExcel();

//        $resultPHPExcel =new \PHPExcel();
        $PHPSheet = $resultPHPExcel->getActiveSheet();
        // 设置个表格标题
        $PHPSheet->setTitle("代理商");
        $PHPSheet->setCellValue("A1", "ID");
        $PHPSheet->setCellValue("B1", "品牌名称");
        $PHPSheet->setCellValue("C1", "排序");
        $PHPSheet->setCellValue("D1", "图片地址");
        $i = 2;
        foreach ($res as $key => $value) {
            $PHPSheet->setCellValue('A' . $i, '' . $value['id']);
            $PHPSheet->setCellValue('B' . $i, '' . $value['bname']);
            $PHPSheet->setCellValue('C' . $i, '' . $value['sort']);

            //获取到图片信息
            $img = DB::table('eb_good')->where('id', $value['id'])->field('image_input')->find();

            $objDrawing[$key] = new \PHPExcel_Worksheet_Drawing();
            $objDrawing[$key]->setPath('.' . $img['image_input']);   //这里拼接 . 是因为要在根目录下获取

            $objDrawing[$key]->setHeight(20);//照片高度
            $objDrawing[$key]->setWidth(70); //照片宽度

            $objDrawing[$key]->setCoordinates('D' . $key);

            // 图片偏移距离
            $objDrawing[$key]->setOffsetX(12);
            $objDrawing[$key]->setOffsetY(12);
            $objDrawing[$key]->setWorksheet($resultPHPExcel->getActiveSheet());
//            $PHPSheet->setCellValue('D'.$i,''.$value['image_input']);
            $i++;
        }
        //设置导出文件名

        $outputFileName = 'total.xls';

        $xlsWriter = new \PHPExcel_Writer_Excel5($resultPHPExcel);

        ob_end_clean();
        header("Content-Type: application/force-download");

        header("Content-Type: application/octet-stream");

        header("Content-Type: application/download");

        header('Content-Disposition:inline;filename="' . $outputFileName . '"');

        header("Content-Transfer-Encoding: binary");

        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

        header("Pragma: no-cache");

        $xlsWriter->save("php://output");

    }

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