laravel导出excel表格中带图片

发布时间 2023-05-30 10:39:55作者: 哟~好莱坞
1.用到扩展
      $dat        = [];
        foreach ($resDB as $k=>$v) {
            $dat[$k] = [
                'style_no'         => $v['style_no'],
                'style_name'       => $v['style_name'],
                'wgt'              => $v['wgt'],
                'metal_prc'        => $v['metal_prc'],
                'metal_amt'        => $v['metal_amt'],
                'mstone'           => $v['mstone'],
                'stone'            => $v['stone'],
                'qty_wage'         => $v['qty_wage'],
                'wgt_wage'         => $v['wgt_wage'],
                'amt'              => $v['amt'],
            ];
        }
        $imgs   = array_column($dat,'style_no');
        $she    = ['K'=>''];

        foreach ($imgs as $k => $v){
            $i = $k+2;
            # 将对象命名为可变变量
            foreach ($she as $kk=>$vv) {
                $subdirectory=$v.'.jpg';
                if(isset($imges[$subdirectory])){
                    $url = 'http://'.$v.'.jpg';
                }else{
                    $url = 'http://no.jpg';
                }
                $$i     = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                $$i->setPath($url);
                $$i->setHeight(80);
                $$i->setCoordinates($kk.$i);
                # 将对象放入数组
                $drawings[] = ${$i};
            }
        }


        $dats = [
            ['款式编号','款式名称','金重','金价','金料额','主石详情','辅石详情','件工费','重工费','总金额','图片'],$dat
        ];

        $export = new QuotePicExport($dats,$drawings);
        # 下载excel文件
        return \Maatwebsite\Excel\Facades\Excel::download($export, $no.'.xls');

2.如果遇到图片地址中携带中文可修改扩展文件

Drawing

 

 
public function setPath($path, $verifyFile = true, $zip = null)
    {
        if ($verifyFile) {
            // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
//            if (filter_var($path, FILTER_VALIDATE_URL)) {
            if (filter_var($path, FILTER_VALIDATE_URL) || preg_match('/http.*?\./',$path)) { 
                $this->path = $path;
                // Implicit that it is a URL, rather store info than running check above on value in other places.
                $this->isUrl = true;
                $path=iconv('utf-8','gb2312',$path); #此处设置字符编码 中文
                $imageContents = file_get_contents($path);
                $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
                if ($filePath) {
                    file_put_contents($filePath, $imageContents);
                    if (file_exists($filePath)) {
                        $this->setSizesAndType($filePath);
                        unlink($filePath);
                    }
                }
            } elseif (file_exists($path)) {
                $this->path = $path;
                $this->setSizesAndType($path);
            } elseif ($zip instanceof ZipArchive) {
                $zipPath = explode('#', $path)[1];
                if ($zip->locateName($zipPath) !== false) {
                    $this->path = $path;
                    $this->setSizesAndType($path);
                }
            } else {
                throw new PhpSpreadsheetException("File $path not found!");
            }
        } else {
            $this->path = $path;
        }

        return $this;
    }

Xls.php

 

 private function processDrawing(BstoreContainer &$bstoreContainer, Drawing $drawing): void
    {
        $blipType = null;
        $blipData = '';
        $filename = $drawing->getPath();
        $filename=iconv('utf-8','gb2312',$filename);  #此处设置字符编码 中文
        [$imagesx, $imagesy, $imageFormat] = getimagesize($filename);

        switch ($imageFormat) {
            case 1: // GIF, not supported by BIFF8, we convert to PNG
                $blipType = BSE::BLIPTYPE_PNG;
                ob_start();
                imagepng(imagecreatefromgif($filename));
                $blipData = ob_get_contents();
                ob_end_clean();

                break;
            case 2: // JPEG
                $blipType = BSE::BLIPTYPE_JPEG;
                $blipData = file_get_contents($filename);

                break;
            case 3: // PNG
                $blipType = BSE::BLIPTYPE_PNG;
                $blipData = file_get_contents($filename);

                break;
            case 6: // Windows DIB (BMP), we convert to PNG
                $blipType = BSE::BLIPTYPE_PNG;
                ob_start();
                imagepng(SharedDrawing::imagecreatefrombmp($filename));
                $blipData = ob_get_contents();
                ob_end_clean();

                break;
        }
        if ($blipData) {
            $blip = new Blip();
            $blip->setData($blipData);

            $BSE = new BSE();
            $BSE->setBlipType($blipType);
            $BSE->setBlip($blip);

            $bstoreContainer->addBSE($BSE);
        }
    }