欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

laravel excel 导出图片

程序员文章站 2022-05-20 21:58:03
...

工作中遇到一个需求,将摄像头抓拍的图片,放入excel中,并导出。找了一大圈,找到一篇文章 点击这里,根据这个自己改动了一些,下面直接上 代码,框架为laravel5.5 

//虚拟数据
$cellData[] = ['学号','姓名','年龄','成绩','名次','图片'];
        $cellData[] = ['10001','林',19,100,1,'face/2018/04/13/thumb/7.png'];
        $cellData[] = ['10001','林',19,100,1,'face/2018/04/13/thumb/2.png'];
        $cellData[] = ['10001','林',19,100,1,'face/2018/04/13/thumb/3.png'];
        $cellData[] = ['10001','林',19,100,1,'face/2018/04/13/thumb/4.png'];
        $cellData[] = ['10001','林',19,100,1,'face/2018/04/13/thumb/5.png'];
        $cellData[] = ['10001','林',19,100,1,'face/2018/04/13/thumb/6.png'];
        
        Excel::create("学生成绩",function ($excel) use ($cellData){
            $excel->sheet('score',function ($sheet) use ($cellData) {
                //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'];
                //遍历数据
                for($i=0;$i<sizeof($cellData);$i++){
                    foreach($cellData[$i] as $k=>$v){
                        //设置图片列高度
                        $i>0 && $sheet->setHeight($i+1, 65);
                        //设置图片列宽度
                        $sheet->setWidth(array('F'=>12));
                        //判断图片列,如果是则放图片
                        if($k == 5 && $i>0){
                            $objDrawing = new PHPExcel_Worksheet_Drawing;
                            $objDrawing->setPath('/app/images/'.$v);
                            $objDrawing->setCoordinates($title_array[$k] . ($i+1));
                            $objDrawing->setHeight(80);
                            $objDrawing->setOffsetX(1);
                            $objDrawing->setRotation(1);
                            $objDrawing->setWorksheet($sheet);
                            continue;
                        }
                        //否则放置文字数据
                        $sheet->cell($title_array[$k] . ($i+1), function ($cell) use ($v) {
                            $cell->setValue($v);
                        });    
                    }                    
                }
            });
        })->export('xls');

导出excel 如下图

laravel excel 导出图片