maatwebsite/excel官方文档
https://docs.laravel-excel.com/3.1/getting-started/
需求
- Laravel Admin 后台上传excel、如果文件符合格式,则转存到自定义的目录,然后入库
laravel Admin截图示例
增加按钮参考:https://blog.csdn.net/Selly166/article/details/105775447
- 导入的弹出框
导入的操作,maatwebsite/excel 用的3.1的版本
- 环境要求:PHP > 7.0,laravel >5.5
- composer安装,如已安装请忽略
composer require maatwebsite/excel
- 安装完成后,创建导入类
php artisan make:import 自定义名称 --model=对应模型路径
//示例
php artisan make:import ImportPhoneCase --model=Models\PhoneCaseModel
- maatwebsite/excel生成的Import类示例代码
<?php
namespace App\Imports;
use App\Models\PhoneCaseRecord;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class PhoneCaseRecordImport implements ToCollection
{
protected $delTitle;
/**
* 删除没用的标题 默认一行
* 我的业务是3行,实例化传参即可
*/
public function __construct($delTitle = 1)
{
$this->delTitle = $delTitle;
}
/**
* @param Collection $rows
* @2020/3/23 9:53
*/
public function collection(Collection $rows)
{
//删除表头
$this->delTitle($rows);
//插入数据库操作
$this->addRecords($rows);
}
//删除无用行
public function delTitle (&$rows) {
$rows = $rows->slice($this->delTitle)->values();
}
//添加记录数
public function addRecords($rows) {
foreach ($rows as $row)
{
$aData = $row->toArray();
//excel经常读取很多空行,如果不判断会导入很多空记录,根据你自己的逻辑去判断,不为空的数据才允许插入;
if (! empty($aData) && ! empty($aData[0]) && ! empty($aData[1])) {
PhoneCaseRecord::create([
'case_title' => $aData[0],
'medlive_id' => $aData[1],
'reviewer' => $aData[2],
'review_status' => $aData[3],
'no_pass_reason' => $aData[4],
'submit_at' => $aData[5],
]);
}
}
}
}
- 使用,上传文件,判断格式、大小、excel表头是否符合规范,符合移动到某个目录,然后插入到数据库,插入前判断为空,
<?php
namespace App\Admin\Actions\Post;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use App\Imports\PhoneCaseRecordImport;
use Maatwebsite\Excel\HeadingRowImport;
class ImportPhoneCase extends Action
{
public $name = '导入病例';
protected $selector = '.import-post';
public function handle(Request $oRequest)
{
if (!$oRequest->hasFile('file')) {
return $this->response()->error('请选择文件后在提交');
}
//获取文件对象
$oFile = $oRequest->file('file');
if ($oFile->isValid()) {
//判断文件扩展是否符合要求
$aAllowExtension = ['xls', 'csv', 'xlsx'];
$sFileExtension = $oFile->getClientOriginalExtension();
if (!in_array($sFileExtension, $aAllowExtension)) {
return $this->response()->error('您上传的文件后缀不支持,支持' . implode(',', $aAllowExtension));
}
//判断大小是否符合20M
if ($oFile->getClientSize() >= 20480000) {
return $this->response()->error('您上传的的文件过大,最大20M');
}
//判断表头是否符合规范
//规范的表头
$aExampleTitle = ['case_title', 'user_id', 'reviewer', 'review_status', 'no_pass_reason', 'submit_at', 'description'];
//使用maatwebsite/excel,获取excel表头
$headings = (new HeadingRowImport)->toArray($oFile);
//判断符合模板表头
if ( ! empty(array_diff( $headings[0][0], $aExampleTitle))) {
return $this->response()->error('上传的excel格式不符合规范,请下载示例后填充数据并上传');
}
//移动到文件夹
$sFilePath = public_path('uploads/phone_case');
if (!is_dir($sFilePath)) {
mkdir($sFilePath, 0775, true);
}
$sFileName = date('YmdHis') . '.' . $sFileExtension;
Storage::disk('admin')->put('phone_case/' . $sFileName, file_get_contents($oFile->getRealPath()));
//excel数据入库,我的excel前3行数据是废的,所以删除掉,代码参考下方
Excel::import(new PhoneCaseRecordImport(3), $sFilePath . '/' . $sFileName);
return $this->response()->success('病例上传成功')->refresh();
}
}
public function form()
{
$this->file('file', '请选择文件');
}
public function html()
{
return <<<HTML
<i class="fa fa-plus"></i>
<a class="btn btn-sm btn-success import-post">导入病例</a>
HTML;
}
}
- 说明,目前遇到的问题
1、上传的excel,反复的编辑,删除末尾行数据,读取的时候,程序会读取到很多空的行
解决:加判断
2、读取excel时间,例如格式为:2020-04-27 11:06:06,读取到432143.43214321,这种格式
解决:下载模板中声明,或和后台管理员沟通,一定要用纯【文本】格式
————————————————
版权声明:本文为CSDN博主「赤三禾」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Selly166/article/details/105784728/