php 导出文件 PhpSpreadshee 带图片

发布时间:2023年12月28日

    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, '参数错误!');
        }
    }
    ```
文章来源:https://blog.csdn.net/qq_19598963/article/details/135264948
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。