🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Laravel maatwebsite/Excel 2.1 文档翻译笔记 参考博客:[https://www.tytrock.com/topics/243](https://www.tytrock.com/topics/243) ## 导入 ``` //获取用户上传的分类文件 $file = $request\->file('classFile'); $ext = $file\->getClientOriginalExtension(); $path = $file\->getRealPath(); //解析用户上传的分类文件 $wpsData = \[\]; Excel::load($path, function ($reader) use (&$wpsData) { $reader\->skipRows(1); // 跳过第一行的中文 $wpsData = $reader\->toArray();        //获取表中的数据             }); //取出第一张sheet的数据 $wpsData\= $wpsData\[0\]; //获取文件内的图片 $classImgInfo\=$this\->getImg($path); if(!empty($classImgInfo)){ foreach($wpsData as $key =>$val){ if(isset($classImgInfo\[$key\])){ $imgPath\=$classImgInfo\[$key\]\[0\]; $sUrl = $this\->upload\_image($imgPath); $wpsData\[$key\]\['img'\]= $sUrl;                     }                 } /\*\*      \* 获取Excel中的图片 \* @author shishuaibo \* @param \[type\] $path      \*/ public function getImg($path){ $inputFileName =$path; $inputFileType = PHPExcel\_IOFactory::identify($inputFileName); $objReader = PHPExcel\_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader\->load($inputFileName); $sheet = $objPHPExcel\->getSheet(0); //$imageFilePath = './uploads/imgs/'; //图片本地存储的路径 $imageFilePath = storage\_path('classImg/'); //图片本地存储的路径 if (!file\_exists($imageFilePath)) { mkdir($imageFilePath, 0777, true);         } $imgInfo\=\[\]; //处理图片 foreach ($sheet\->getDrawingCollection() as $img) { list($startColumn, $startRow) = PHPExcel\_Cell::coordinateFromString($img\->getCoordinates()); //获取图片所在行和列 $imageFileName = $img\->getCoordinates() . mt\_rand(1000, 9999); switch($img\->getExtension()) { case 'jpg': case 'jpeg': $imageFileName .= '.jpeg'; $source = imagecreatefromjpeg($img\->getPath()); imagejpeg($source, $imageFilePath.$imageFileName); break; case 'gif': $imageFileName .= '.gif'; $source = imagecreatefromgif($img\->getPath()); imagejpeg($source, $imageFilePath.$imageFileName); break; case 'png': $imageFileName .= '.png'; $source = imagecreatefrompng($img\->getPath()); imagejpeg($source, $imageFilePath.$imageFileName); break;             } $startColumn = $this\->ABC2decimal($startColumn); $imgInfo\[$startRow\-3\]\[$startColumn\-7\] = $imageFilePath . $imageFileName;         } return $imgInfo;     } public function ABC2decimal($abc)     { $ten = 0; $len = strlen($abc); for($i\=1;$i<=$len;$i++){ $char = substr($abc,0\-$i,1);//反向获取单个字符 $int = ord($char); $ten += ($int\-65)\*pow(26,$i\-1);         } return $ten;     } ``` ## 导出 ``` Excel::create($fileName, function($excel) use($arrDatas, $sheet\_name){ //去除特殊字符 $sheet\_name\=$this\->str\_re($sheet\_name); $sheet\_name\='分类数据'; $excel\->sheet($sheet\_name, function($sheet) use($arrDatas){   $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'\];     $sheet\->fromArray($arrDatas);   //处理图片 foreach($arrDatas as $key\=>$item){ if($key\>0 && !empty($item\['img'\])){ $sheet\->setCellValue('H'.($key+2), ''); $img = $this\->curlGet($item\['img'\]); $dir = public\_path('/export/classImg/'); $file\_info = pathinfo($item\['img'\]); if (!empty($file\_info\['basename'\])) { //过滤非文件类型 $basename = $file\_info\['basename'\]; is\_dir($dir) OR mkdir($dir, 0777, true); //进行检测文件是否存在 file\_put\_contents($dir . $basename, $img); $obj\= new PHPExcel\_Worksheet\_Drawing(); //使用phpExcel                           $obj\->setPath($dir . $basename); $sp\= $title\_array\[0 + 3\]; $obj\->setCoordinates('H'.($key+2));//设置图片要插入的单元格 $sheet\->setHeight($key\+ 2,65);//设置高度 $sheet\->setWidth(array($sp\=> 11));//设置宽度 $obj\->setHeight(80); $obj\->setOffsetX(1);    //设置偏移量 $obj\->setRotation(1); $obj\->setWorksheet($sheet);                             }                         }                     }                   });             })->store('xlsx',public\_path('/export/class/'));  #导出格式为xlsx //将文件上传到阿里云上,url地址返回 $result\['url'\] = $this\->upload\_image(public\_path('/export/class/').$fileName.'.xlsx'); //curl请求 public function curlGet($url)     { $ch = curl\_init(); curl\_setopt($ch, CURLOPT\_URL, $url); curl\_setopt($ch, CURLOPT\_HEADER, 0); curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($ch, CURLOPT\_SSL\_VERIFYPEER, false); // 这个是重点 请求https。 $data = curl\_exec($ch); curl\_close($ch); return $data;     } ```