php 导出excel 带图片

php 复制代码
    /**
     * 导出
     */
    public function export()
    {
        $where = [];
        $fullname = $this->request->param("fullname");
        $status = $this->request->param("status");

        $start_time = $this->request->param("start_time");
        $end_time = $this->request->param("end_time");

        $fullname && $where[] = ['partner.fullname','LIKE',"%{$fullname}%"];
        $status && $where[] = ['feedback.status','=',$status];

        if ($start_time != '' && $end_time != ''){
            $where[] = ['feedback.create_time','between',[$start_time, $end_time]];
        }


        $fields = [
            "subuser.user_name",
            "subuser.partner_id",
            "feedback.feedback_id",
            "feedback.text",
            "feedback.files",
            "feedback.status",
            "feedback.create_time",
            "feedback.reply_time",
            "feedback.reply_text",
            "partner.fullname",
        ];
        $list = $this->model->alias("feedback")
            ->leftjoin("subuser subuser","feedback.subuser_id = subuser.subuser_id")
            ->leftjoin("partner partner","partner.partner_id = subuser.partner_id")
            ->where($where)
            ->field($fields)
            ->order('feedback.feedback_id DESC')
            ->select()
            ->toArray();
        
        //开始写入EXCEL
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(root_path() . 'public/tpl/意见反馈导出模板.xls');//读取指定路径下的模板
        $worksheet = $spreadsheet->getActiveSheet();//指向激活的工作表
        $worksheet->setTitle('Product catalog');
        $count=count($list);
        $worksheet->insertNewRowBefore(3,$count-1);



        $startline=2;
        //遍历表单数据
        $path = root_path() . 'public/tmpxls/shop_feedback/';
        @mkdir($path);

        foreach($list as $k=>$v){
            $line=$k+$startline;

            // 判断是不是图片
            $img_url = $v['files_text'][0] ?? '';
            $check_img = $this->model->isImageURL($img_url);

            if ($check_img == true){

                $fileInfo = pathinfo($img_url);
                $file =  $fileInfo['filename'] .'.'.$fileInfo['extension'];

                //下载图片到本地
                if (file_exists( $path.$file) === false) {
                    copy($img_url, $path.$file);
                }

                if (!file_exists($path.$file)) {
                    // 跳过无效图片
                    continue;
                }

                //下载图片到本地
                if(file_exists($path.$file)===true){

                    $drawing =new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                    $drawing->setName('Image');
                    $drawing->setDescription('Image');
                    $drawing->setPath($path.$file); // 设置图片路径
                    $drawing->setWidth(80); // 设置图片宽度
                    $drawing->setHeight(80); // 设置图片高度
                    $drawing->setCoordinates('E'.$line); // 指定单元格
                    $drawing->setOffsetX(10); // 设置横向偏移
                    $drawing->setOffsetY(10); // 设置纵向偏移
                    $drawing->setWorksheet($spreadsheet->getActiveSheet());
                }

            }

            //插入行项目
            $worksheet->getCell('A'.$line)->setValue(++$k);
            $worksheet->getCell('B'.$line)->setValue($v['fullname']);
            $worksheet->getCell('C'.$line)->setValue($v['user_name']);
            $worksheet->getCell('D'.$line)->setValue($v['text']);
            $worksheet->getCell('F'.$line)->setValue($v['create_time']);
            $worksheet->getCell('G'.$line)->setValue($v['status_text']);
            $worksheet->getCell('H'.$line)->setValue($v['reply_text']);



        }

        $filename = date("YmdHis").'意见反馈';


        //下载文档
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename='.$filename.'.Xlsx');
        header('Cache-Control: max-age=0');
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        return $writer->save('php://output');





    }

文件模板

相关推荐
hlsd#2 分钟前
go web单体项目 学习总结
开发语言·学习·golang
为什么每天的风都这么大3 分钟前
编译faiss的C++ API
开发语言·c++·faiss
nameofworld7 分钟前
前端面试笔试(五)
开发语言·前端·javascript·面试·递归
BIM云平台开发11 分钟前
关于return,yield 和 yield return
java·开发语言·数据结构·c#
小柯J桑_11 分钟前
C++:用红黑树封装map与set-2
开发语言·c++·set·map·红黑树
zwm_yy23 分钟前
Dockerfile && docker-compose
开发语言·docker
敲代码不忘补水42 分钟前
Python Matplotlib 经典 3D 绘图类型:从二维到三维的可视化解析
开发语言·python·3d·数据分析·numpy·pandas·matplotlib
码农飞飞1 小时前
详解Rust字符串用法
开发语言·算法·rust·string·所有权·字符串用法
流着口水看上帝1 小时前
SAP开发语言ABAP开发入门
开发语言
weixin_431470861 小时前
文本数据分析(nlp)
开发语言·python·深度学习·自然语言处理