github:https://github.com/Maatwebsite/laravel-excel-docs
Exporting collections | Laravel Excel
Reading and writing to file - PhpSpreadsheet Documentation
读取excel数据
# 获取文件
if(!$file = app(Request::class)->file('file')){
return '请上传文件';
}
# 后缀判断
$ext = $file->getClientOriginalExtension();
if ($ext != 'xlsx') {
return '请将文件转成xlsx格式';
}
# 读取数据
$import = new UserImport();
$datas = Excel::toArray($import, $file);
这里用到了的UserImport类
<?php
namespace Common\Imports;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class GiftCdKeyImport implements ToArray,WithHeadingRow,WithCustomCsvSettings
{
public $params;
public function setParams($params){
$this->params = $params;
}
public function Array(Array $tables)
{
return $tables;
}
public function createData($data)
{
}
public function headingRow(): int
{
return 1;
}
public function getCsvSettings(): array
{
return [
'input_encoding' => 'GBK' // 设置编码为GBK格式
];
}
}
另一个excel操作库:Reading Files - PhpSpreadsheet Documentation