PhpSpreadsheet处理表格
作者:互联网
介绍:
PhpSpreadsheet是PHPExcel的下一个版本。它打破了兼容性,大大提高了代码库质量(命名空间,PSR合规性,最新PHP语言功能的使用等)。
由于所有努力都转移到了PhpSpreadsheet,因此将不再维护PHPExcel。PHPExcel,补丁和新功能的所有贡献都应该针对PhpSpreadsheet开发分支。
前提:TP5项目中已经安装配置好Composer 管理工具包。
安装:
命令 composer require phpoffice/phpspreadsheet
前端上传页面:
1 <!DOCTYPE html> 2 <html lang="zh-CN"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <title>index</title> 8 <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 9 <!--[if lt IE 9]> 10 <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script> 11 <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script> 12 <![endif]--> 13 </head> 14 <body> 15 16 <div class="container"> 17 18 <form action="http://test.tp51.com/index/index/uploadexcel" enctype="multipart/form-data" method="post"> 19 <div class="form-group"> 20 <label for="exampleInputFile">选择文件</label> 21 <input type="file" id="excelFile" name="file" accept=".csv,.xls,.xlsx"> 22 <p class="help-block">请选择表格文件,支持 csv,xls,xlsx 格式</p> 23 </div> 24 25 <div class="form-group"> 26 <label for="columm" class="control-label">选择总列数</label> 27 <select class="form-control" name="columm" id="columm"><option>A</option><option>B</option><option>C</option><option>D</option><option>E</option><option>F</option><option>G</option><option>H</option><option>I</option><option>J</option><option>K</option><option>L</option><option>M</option><option>N</option></select> 28 </div> 29 30 <div class="form-group"> 31 <button type="submit" class="btn btn-default">上传</button> 32 </div> 33 </form> 34 35 </div> 36 37 <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> 38 <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 39 40 <script type="text/javascript"> 41 <!-- 42 $("button[type='submit']").on("click",function(){ 43 var filestr = $("#excelFile").val(); 44 var filemate = getFilemate(filestr); 45 console.log(filemate); 46 if (filemate =='xls' || filemate =='xlsx' || filemate =='csv') { 47 console.log("文件正确"); 48 $("form").submit(); 49 }else{ 50 alert("文件格式不正确"); 51 return false; 52 } 53 }); 54 55 //获取文件格式-后缀 56 function getFilemate(o){ 57 var index = o.lastIndexOf(".") 58 if (index==-1) {return false;} 59 return o.substring(index+1); 60 } 61 //--> 62 </script> 63 64 </body> 65 </html>
后端接收处理:
1 <?php 2 namespace app\index\controller; 3 4 use think\Db; 5 use PhpOffice\PhpSpreadsheet\Spreadsheet; 6 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; 7 8 class Index 9 { 10 public function index(){ 11 return view(); 12 } 13 14 //上传表格文件 15 public function uploadexcel(){ 16 $input_columm = input('post.columm/s'); 17 $file = request()->file("file"); 18 $info = $file->validate(['size'=>5242880,'ext'=>'csv,xls,xlsx'])->rule("date")->move("./uploads/DMH",$this->setupname()); 19 if ($info) { 20 //上传成功,处理表格文件 21 $data = array(); 22 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader(ucfirst($info->getExtension())); 23 $spreadsheet = $reader->load($info->getPathname()); 24 $sheet = $spreadsheet->getSheet(0); // 读取第一個工作表 25 $highest_row = $sheet->getHighestRow(); // 取得总行数 26 //$highest_columm = $sheet->getHighestColumn(); // 取得总列数 27 $highest_columm = $input_columm ? $input_columm : $sheet->getHighestColumn(); // 总列数,根据实际情况修改 28 for ($row = 1; $row <= $highest_row; $row++){ //行号从1开始 29 $arr = array(); 30 for ($column = 'A'; $column <= $highest_columm; $column++){ //列数是以A列开始 31 $str = $sheet->getCell($column . $row)->getValue(); 32 array_push($arr,$str); 33 } 34 if (array_filter($arr)) { 35 $data[$row] = array_filter($arr); 36 } 37 } 38 //var_dump($data); 39 return view('excel',['data'=>$data]); 40 } else { 41 echo $file->getError(); 42 } 43 } 44 45 //导出excel 46 public function outexcel(){ 47 $spreadsheet = new Spreadsheet(); 48 $sheet = $spreadsheet->getActiveSheet(); 49 $sheet->setCellValue('A1', 'Hello World !'); 50 $writer = new Xlsx($spreadsheet); 51 $writer->save('hello world2.xlsx'); 52 } 53 54 55 56 }
标签:info,sheet,表格,处理,spreadsheet,PhpSpreadsheet,data,row 来源: https://www.cnblogs.com/lijea/p/10350477.html