php实现excel导出并插入图片

php实现excel导出并插入图片

Ezra
2022-06-18 / 0 评论 / 191 阅读 / 正在检测是否收录...
use Maatwebsite\Excel\Facades\Excel;
use PHPExcel_Worksheet_Drawing;

public function index1(){
        
        //定义文件名称为日期拼上uniqid()
        $fileName = date('YmdHis') . '-' . uniqid();

        Excel::create($fileName, function($excel) {
            $excel->sheet('sheet1', function($sheet) {
                // 这段逻辑是从表格数据中取出需要导出的字段
                $head = ['id', '名称', '类型', '二维码', '上传人',"第二张图"];
                $body = ['id', 'name', 'type', 'url', 'admin',"dier"];
                $data = [
                ['id'=>'id1', 'name'=>'name1', 'type'=>'type1', 'url'=>"/2022-01-25/1UHXtn.jpg","dier"=>"/2022-01-25/PZ1cdD.jpg", 'admin'=>"admin1"],
                ['id'=>'id2', 'name'=>'name2', 'type'=>'type2', 'url'=>"/2022-01-25/eJ3QHO.jpg","dier"=>"/2022-01-25/XCXs7M.jpg", 'admin'=>"admin2"],
                ['id'=>'id3', 'name'=>'name3', 'url'=>"/2022-01-25/Lx4gGV.jpg", 'type'=>'type3',"dier"=>"/2022-01-25/njFtcG.jpg", 'admin'=>"admin3"],
                ];
                //init列
                $title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                    'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'];

                $rows = collect([$head]); //写入标题
                $sheet->rows($rows);
                collect( $data )->map( function ($item,$k)use($body,$sheet,$title_array ) {
                    foreach ($body as $i=>$keyName){
                        if($keyName == 'url') { //判断图片列,如果是则放图片
                            $objDrawing = new PHPExcel_Worksheet_Drawing;
                            // $v = public_path('/upload/'). array_get($item, $keyName); //拼接图片地址
                            $v = public_path('/upload/images'). array_get($item, $keyName); //拼接图片地址
                            $objDrawing->setPath( $v );
                            $sp = $title_array[$i];
                            $objDrawing->setCoordinates( $sp . ($k+2) );
                            $sheet->setHeight($k+2, 65); //设置高度
                            $sheet->setWidth(array( $sp =>12));  //设置宽度
                            $objDrawing->setHeight(80);
                            $objDrawing->setOffsetX(1);
                            $objDrawing->setRotation(1);
                            $objDrawing->setWorksheet($sheet);
                        }elseif($keyName == "dier"){
                            $objDrawing = new PHPExcel_Worksheet_Drawing;
                            // $v = public_path('/upload/'). array_get($item, $keyName); //拼接图片地址
                            $v = public_path('/upload/images'). array_get($item, $keyName); //拼接图片地址
                            $objDrawing->setPath( $v );
                            $sp = $title_array[$i];
                            $objDrawing->setCoordinates( $sp . ($k+2) );
                            $sheet->setHeight($k+2, 65); //设置高度
                            $sheet->setWidth(array( $sp =>12));  //设置宽度
                            $objDrawing->setHeight(80);
                            $objDrawing->setOffsetX(1);
                            $objDrawing->setRotation(1);
                            $objDrawing->setWorksheet($sheet);
                        } else { //否则放置文字数据
                            $v = array_get($item, $keyName);
                            $sheet->cell($title_array[$i] . ($k+2), function ($cell) use ($v) {
                                $cell->setValue($v);
                            });
                        }
                    }
                });
            });
        })
        ->store('xls')//服务器保存一份
        ->export('xls');//浏览器下载一份
        
        echo $fileName;
    }

l4k0zb8t.png

0

评论 (0)

取消