<div class="layuimini-container"> <form id="app-form" class="layui-form layuimini-form"> <div class="layui-form-item"> <label class="layui-form-label">導入excel:</label> <div class="layui-input-block"> <button type="button" class="layui-btn" id="test1"> <i class="layui-icon"></i>上傳文件 </button> </div> </div> </form> </div> <script src="/static/plugs/layui-v2.5.6/layui.all.js?v=2.0.0" charset="utf-8"></script> <script src="/static/plugs/jquery-3.4.1/jquery-3.4.1.min.js?v=2.0.0" charset="utf-8"></script> <script> layui.use('upload', function(){ var upload = layui.upload; var loading; //執行實例 var uploadInst = upload.render({ elem: '#test1' //綁定元素 ,url: '/admin/mall.goodsone/import' //上傳接口 ,accept: 'file' ,choose:function(){ loading = layer.load(0, { shade: false, time: 60*60*1000 }); } ,done: function(res){ //上傳完畢回調 layer.close(loading); if(res.msg == '導入成功'){ layer.msg(res.msg); } } ,error: function(){ layer.close(loading); //請求異常回調 } }); }); </script>
use EasyAdmin\tool\CommonTool; use jianyan\excel\Excel;
$file = request()->file('file'); ini_set('memory_limit','1024M'); $data = Excel::import($file);
composer require phpoffice/phpspreadsheet=1.8.2
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; $imageFilePath = './uploads/imgs/'; //圖片本地存儲的路徑 if (!file_exists($imageFilePath)) { //若是目錄不存在則遞歸建立 mkdir($imageFilePath, 0777, true); } try { $inputFileName = './files/1.xlsx'; //包含圖片的Excel文件 $objRead = IOFactory::createReader('Xlsx'); $objSpreadsheet = $objRead->load($inputFileName); $objWorksheet = $objSpreadsheet->getSheet(0); $data = $objWorksheet->toArray(); foreach ($objWorksheet->getDrawingCollection() as $drawing) { list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates()); $imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999); switch ($drawing->getExtension()) { case 'jpg': case 'jpeg': $imageFileName .= '.jpg'; $source = imagecreatefromjpeg($drawing->getPath()); imagejpeg($source, $imageFilePath . $imageFileName); break; case 'gif': $imageFileName .= '.gif'; $source = imagecreatefromgif($drawing->getPath()); imagegif($source, $imageFilePath . $imageFileName); break; case 'png': $imageFileName .= '.png'; $source = imagecreatefrompng($drawing->getPath()); imagepng($source, $imageFilePath, $imageFileName); break; } $startColumn = ABC2decimal($startColumn); $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName; } dump($data);die(); } catch (\Exception $e) { throw $e; } 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; }
use PHPExcel_IOFactory; use PHPExcel_Cell; try { $inputFileName = './files/1.xlsx'; $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); } catch (\Exception $e) { die('加載文件發生錯誤:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); } $sheet = $objPHPExcel->getSheet(0); $data = $sheet->toArray(); //該方法讀取不到圖片,圖片需單獨處理 $imageFilePath = './uploads/imgs/'; //圖片本地存儲的路徑 if (!file_exists($imageFilePath)) { mkdir($imageFilePath, 0777, true); } //處理圖片 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 = ABC2decimal($startColumn); $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName; } var_dump($data); 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; }