PhpSpreadsheet处理表格

时间:2023-01-17 18:30:29

介绍:
PhpSpreadsheet是PHPExcel的下一个版本。它打破了兼容性,大大提高了代码库质量(命名空间,PSR合规性,最新PHP语言功能的使用等)。
由于所有努力都转移到了PhpSpreadsheet,因此将不再维护PHPExcel。PHPExcel,补丁和新功能的所有贡献都应该针对PhpSpreadsheet开发分支。
前提:TP5项目中已经安装配置好Composer 管理工具包。

安装:
命令 composer require phpoffice/phpspreadsheet

前端上传页面:

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>index</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
</head>
<body> <div class="container"> <form action="http://test.tp51.com/index/index/uploadexcel" enctype="multipart/form-data" method="post">
<div class="form-group">
<label for="exampleInputFile">选择文件</label>
<input type="file" id="excelFile" name="file" accept=".csv,.xls,.xlsx">
<p class="help-block">请选择表格文件,支持 csv,xls,xlsx 格式</p>
</div> <div class="form-group">
<label for="columm" class="control-label">选择总列数</label>
<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>
</div> <div class="form-group">
<button type="submit" class="btn btn-default">上传</button>
</div>
</form> </div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script type="text/javascript">
<!--
$("button[type='submit']").on("click",function(){
var filestr = $("#excelFile").val();
var filemate = getFilemate(filestr);
console.log(filemate);
if (filemate =='xls' || filemate =='xlsx' || filemate =='csv') {
console.log("文件正确");
$("form").submit();
}else{
alert("文件格式不正确");
return false;
}
}); //获取文件格式-后缀
function getFilemate(o){
var index = o.lastIndexOf(".")
if (index==-1) {return false;}
return o.substring(index+1);
}
//-->
</script> </body>
</html>

后端接收处理:

 <?php
namespace app\index\controller; use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class Index
{
public function index(){
return view();
} //上传表格文件
public function uploadexcel(){
$input_columm = input('post.columm/s');
$file = request()->file("file");
$info = $file->validate(['size'=>5242880,'ext'=>'csv,xls,xlsx'])->rule("date")->move("./uploads/DMH",$this->setupname());
if ($info) {
//上传成功,处理表格文件
$data = array();
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader(ucfirst($info->getExtension()));
$spreadsheet = $reader->load($info->getPathname());
$sheet = $spreadsheet->getSheet(0); // 读取第一個工作表
$highest_row = $sheet->getHighestRow(); // 取得总行数
//$highest_columm = $sheet->getHighestColumn(); // 取得总列数
$highest_columm = $input_columm ? $input_columm : $sheet->getHighestColumn(); // 总列数,根据实际情况修改
for ($row = 1; $row <= $highest_row; $row++){ //行号从1开始
$arr = array();
for ($column = 'A'; $column <= $highest_columm; $column++){ //列数是以A列开始
$str = $sheet->getCell($column . $row)->getValue();
array_push($arr,$str);
}
if (array_filter($arr)) {
$data[$row] = array_filter($arr);
}
}
//var_dump($data);
return view('excel',['data'=>$data]);
} else {
echo $file->getError();
}
} //导出excel
public function outexcel(){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world2.xlsx');
} }