php 导出文件 PhpSpreadshee 带图片


    function ProductListExport(){
        $user_id=$this->request->uid;
        $USER=getAdminInfo($user_id);
        if(!array_intersect(explode(",",$USER['role_id']),[1,2,3,4])){
            return $this->ajaxReturn($this->errorCode, '你没有权限访问,如有需要请联系管理员!');
        }
        $postField = 'pdid';
        $req = $this->request->only(explode(',',$postField),'post',null);
        $product_ids =array_column($req['pdid'],'product_id');
        $priceheads =array_column($req['pdid'],'pricehead');
        $pricehead = array_pop($priceheads);
        $pricehead = $pricehead == '' ?'价格':$pricehead;
        $product_prices =array_column($req['pdid'],'price','product_id');

        if($req['pdid']){
            //取产品数据
            $list=db('product')->alias('a')
                ->leftjoin('cd_brand b','a.brand_id=b.brand_id')
                ->leftjoin('cd_product_company_link c','a.product_id=c.product_id and c.company_id=1')
                ->leftjoin('cd_user d','c.buyer_id=d.user_id')
                ->field('a.product_id,a.l_name_zh as pdname,a.l_name_en,a.l_model_zh,a.engines,a.frey_no,a.number,b.l_name_zh,d.name')
                ->where('a.product_id','in',$product_ids)
                ->order('product_id')
                ->select()
                ->toArray();
            //开始写入EXCEL
            $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(root_path() . 'public/tmpxls/产品列表导出目录.xls');//读取指定路径下的模板
            $worksheet = $spreadsheet->getActiveSheet();//指向激活的工作表
            $worksheet->setTitle('Product catalog');
            $count=count($list);
            $worksheet->insertNewRowBefore(3,$count-2);
            $startline=2;
            //遍历表单数据
            $path = root_path() . 'public/tmpxls/pic/';
            @mkdir($path);
            //价格 标头显示处理
            $worksheet->getCell('L1')->setValue($pricehead);

            foreach($list as $k=>$v){

                $kc = \app\api\service\Crm\ProductService::productstock($v['product_id']);
                $line=$k+$startline;

                //下载图片到本地
                if (file_exists( $path. $v['frey_no'] . '.png') === false) {
                    $product_img = \app\api\unit\Common::PIC_URL_HTTP . '?f=37/' . \frey\FreyImage::encrypt($v['frey_no']).'&n=0&u=200&c=1';
                    copy($product_img, $path . $v['frey_no'] . '.png');
                }

                //下载图片到本地
                if(file_exists($path.$v['frey_no'].'.png')===true){
                    //插入图片
                    $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                    $drawing->setName($v['frey_no']);
                    $drawing->setDescription($v['frey_no']);
                    $drawing->setPath(root_path().'public/tmpxls/pic/'.$v['frey_no'].'.png');
                    $drawing->setWidth(80);
                    $drawing->setHeight(80);
                    $drawing->setCoordinates('A'.$line);
                    $drawing->setOffsetX(13);
                    $drawing->setOffsetY(6);
                    $drawing->setWorksheet($spreadsheet->getActiveSheet());
                }
                //插入行项目
                $worksheet->getCell('B'.$line)->setValue($v['pdname']);//产品中文名称
                $worksheet->getCell('C'.$line)->setValue($v['l_name_en']);//产品英文名称
                $worksheet->getCell('D'.$line)->setValue($v['l_model_zh']);//车型
                $worksheet->getCell('E'.$line)->setValue($v['engines']);//发动机
                $worksheet->getCell('F'.$line)->setValue($v['l_name_zh']);//产品品牌
                $worksheet->getCell('G'.$line)->setValue($v['frey_no']);//菲尔号
                $worksheet->getCell('H'.$line)->setValue($v['number']);//OEM号
                $worksheet->getCell('I'.$line)->setValue($kc['kykc']);//库存
                $worksheet->getCell('J'.$line)->setValue($kc['ZTQTY']);//在途库存
                $worksheet->getCell('K'.$line)->setValue($v['name']);//采购员
                $price = $product_prices[$v['product_id']] ??0;
                $worksheet->getCell('L'.$line)->setValue($price);//价格
            }
            //下载文档
            $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');
        }else{
            return $this->ajaxReturn($this->errorCode, '参数错误!');
        }
    }
    ```
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PhpSpreadsheet可以将Excel表格中的图片导出,只需要在导出Excel时对图片进行处理即可。 首先需要将图片插入到Excel表格中,可以使用`PhpSpreadsheet\Worksheet\Drawing`类来实现。例如: ```php use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; // 创建一个 Drawing 对象 $drawing = new Drawing(); $drawing->setName('Logo'); $drawing->setDescription('Logo'); $drawing->setPath('path/to/image.jpg'); $drawing->setHeight(50); $drawing->setCoordinates('A1'); // 将 Drawing 对象添加到工作表中 $worksheet->getRowDimension(1)->setRowHeight(80); $worksheet->getColumnDimension('A')->setWidth(30); $worksheet->setCellValue('A1', '这是一个图片的单元格'); $worksheet->setDrawing($drawing); ``` 上面的代码中,我们创建了一个Drawing对象,并设置了图片的路径、高度、位置等属性。然后将Drawing对象添加到工作表中,再将单元格的值设置为图片的描述。 接下来,可以使用`PhpSpreadsheet\IOFactory`类将工作表导出为Excel文件。例如: ```php use PhpOffice\PhpSpreadsheet\IOFactory; // 创建一个工作簿对象 $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); // 插入图片到工作表中 // 导出Excel文件 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('path/to/exported.xlsx'); ``` 这样就可以将图片的Excel表格导出了。需要注意的是,导出的Excel文件中可能会出现图片失真或者位置偏移的情况,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值