php Yii2 Excel导出图片资源到表格

php 复制代码
public static function exportSample($headers, $data, $fileName = 'excel', $writeType = 'Xls')
    {

        $img_field = ['sam_img','ref_img'];
        $_img_field_width = 16;
        $_img_field_height = 80;

        $excel = new Spreadsheet();
        $sheet = $excel->setActiveSheetIndex(0);

        // 设置标题单元格的样式:字体加粗,字体大小12,字体颜色红色,水平居中,垂直居中
        $styleArray = [
            'font' => [ //设置字体
                'bold' => true,
                'size' => 9,
//                'color' => array('rgb' => 'FF0000'),
            ],
            'alignment' => [ //对其方式
                'horizontal' => Excel::HORIZONTAL_CENTER, //居中
                'vertical' => Excel::VERTICAL_CENTER,
                'wrapText' => true, // 设置自动换行
            ],
            'fill'=>[ //设置表格的背景色
                'fillType' => Excel::FILL_SOLID,
                'startColor'=>['rgb' => 'B5C6EA'], //设备背景色
            ]
        ];

        $sheet->getStyle('A1:AC2')->applyFromArray($styleArray);

        $styleArray = [
            'borders' => [ //设备边框
                'outline' => [
                    'borderStyle' => Excel::BORDER_THIN, // 边框样式
                    'color' => ['rgb' => '000000'], // 边框颜色为黑色
                ],
            ],
            'alignment' => [ //对其方式
                'horizontal' => Excel::HORIZONTAL_CENTER, //居中
                'vertical' => Excel::VERTICAL_CENTER,
                'wrapText' => true, // 设置自动换行
            ],
        ];

        //循环两次-后面合并需要
        for ($i=1; $i<=2;$i++){
            // 设置标题
            $ordA = ord('A'); //65
            $key2 = ord("@"); //64
            foreach ($headers as $index => $title) {
                if ($ordA > ord("Z")) {
                    $colum = chr(ord("A")) . chr(++$key2); //超过26个字母 AA1,AB1,AC1,AD1...BA1,BB1...
                } else {
                    $colum = chr($ordA++);
                }
                $_p = $colum . $i;

                //写入标题并且设置样式
                $sheet->setCellValue($_p, $title)->getStyle($_p)->applyFromArray($styleArray);

                //图片单独处理宽度
                if(in_array($index,$img_field)){
                    $sheet->getColumnDimension($colum)->setWidth($_img_field_width);
                }
            }
        }

        //合并单元格-列-切重写
        $sheet->mergeCells('D1:M1')->setCellValue('D1', '计划产品性能参数');
        $sheet->mergeCells('N1:S1')->setCellValue('N1', '对标品牌产品性能参数');

        //合并单元格-行
        $h = ['A','B','C','T','U','V','W','X','Y','Z','AA','AB','AC'];
        foreach ($h as $k=>$y){
            $sheet->mergeCells($y.'1:'.$y.'2');
        }

        //图片地址
        $path = \Yii::getAlias('@excelImg');
        self::mkdirFile($path);

        $r = 3; //数据写入航标
        foreach ($data as $dKey => $log) {
            $column = 0;
            foreach ($headers as $kk => $vv) {
                $column++;

                $value = $log[$kk];
                if($kk == 'id'){
                    $value = $column;
                }

                if(in_array($kk,['sam_img','ref_img'])){

                    $_map = [
                        'sam_img'=>'L',
                        'ref_img'=>'M',
                    ];

                    $img_url = $log[$kk];
                    if(empty($img_url)){
                        continue;
                    }
                    $fileInfo = pathinfo($img_url);
                    $file =  $fileInfo['filename'] .'.'.$fileInfo['extension'];

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


                    if (file_exists($new_img_url)) {
                        $drawing = new Drawing();
                        $drawing->setPath($new_img_url);

                        $_rp = $_map[$kk].$r;
                        $drawing->setName('Image');
                        $drawing->setDescription('Image');
                        $drawing->setPath($new_img_url); // 设置图片路径
                        $drawing->setWidthAndHeight(80,80); // 设置图片宽度
                        $drawing->setCoordinates($_rp); // 指定单元格

                        // 设置图片格式选项,例如使其居中
                        $drawing->setResizeProportional(false); // 如果需要保持图片宽高比,设置为false

//                        $drawing->setOffsetX(5); // 设置横向偏移
//                        $drawing->setOffsetY(5); // 设置纵向偏移

                        $sheet->getRowDimension($r)->setRowHeight($_img_field_height);
                        $drawing->setWorksheet($sheet);
                    }

                }else {
                    $sheet->setCellValueExplicitByColumnAndRow($column, $r, $value, DataType::TYPE_STRING2);
                }
            }
            $r++;
        }
        $sheet->freezePane('A3'); //锁定行
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename=' . $fileName . date('Y-m-d') . '.xls');
        header('Cache-Control: max-age=0');
        $excelWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($excel, $writeType);
        $excelWriter->save('php://output');
        die();
    }


public static function mkdirFile($imageDir,$makeDir=1)
    {
        if (! file_exists($imageDir)) {
            if($makeDir){
                $a = mkdir($imageDir,0777,true);
            }else{
                echo '图片目录['.$imageDir.']不存在!';
                exit();
            }
        }
    }

表格效果

图片居中暂时还没找到非常好的实现方式,有经验的大佬欢迎指导

相关推荐
JaguarJack1 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo1 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack2 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay3 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954483 天前
CTF 伪协议
php
BingoGo5 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack5 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo6 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack6 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack7 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端