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');





    }

文件模板

相关推荐
进击的雷神1 小时前
Excel 实现文本拼接方法
excel
东京老树根1 小时前
Excel 技巧15 - 在Excel中抠图头像,换背景色(★★)
笔记·学习·excel
ByteBlossom6662 小时前
MDX语言的语法糖
开发语言·后端·golang
肖田变强不变秃3 小时前
C++实现矩阵Matrix类 实现基本运算
开发语言·c++·matlab·矩阵·有限元·ansys
规划GIS会3 小时前
CC工具箱使用指南:【Excel点集转面要素(批量)】
excel·二次开发·arcgis pro
沈霁晨3 小时前
Ruby语言的Web开发
开发语言·后端·golang
小兜全糖(xdqt)3 小时前
python中单例模式
开发语言·python·单例模式
DanceDonkey3 小时前
@RabbitListener处理重试机制完成后的异常捕获
开发语言·后端·ruby
Python数据分析与机器学习3 小时前
python高级加密算法AES对信息进行加密和解密
开发语言·python
军训猫猫头3 小时前
52.this.DataContext = new UserViewModel(); C#例子 WPF例子
开发语言·c#·wpf