laravel-admin / Dcat admin 上传Excel并导入数据到数据库
本文转自于链接:https://blog.csdn.net/qq_42468039/article/details/108277599
准备工作
安装maatwebsite/excel
composer require maatwebsite/excel
laravel-admin 效果图
1.创建按钮
$grid->tools(function (Grid\Tools $tools) {
// excle 导入
$tools->append(new ExcelAdd());
});
2.创建按钮文件
file('file'));
} catch (ValidationException $validationException) {
return Response::withException($validationException);
} catch (Throwable $throwable) {
$this->response()->status = false;
return $this->response()->swal()->error($throwable->getMessage());
}
return $this->response()->success('上传成功')->refresh();
}
// 按钮样式
public function html()
{
return <<上传简历
HTML;
}
// 上传表单
public function form()
{
$this->file('file', '上传简历')->rules('required', ['required' => '文件不能为空']);
}
/**
* @return string
* 上传效果
*/
public function handleActionPromise()
{
$resolve = <<<'SCRIPT'
var actionResolverss = function (data) {
$('.modal-footer').show()
$('.tips').remove()
var response = data[0];
var target = data[1];
if (typeof response !== 'object') {
return $.admin.swal({type: 'error', title: 'Oops!'});
}
var then = function (then) {
if (then.action == 'refresh') {
$.admin.reload();
}
if (then.action == 'download') {
window.open(then.value, '_blank');
}
if (then.action == 'redirect') {
$.admin.redirect(then.value);
}
};
if (typeof response.html === 'string') {
target.html(response.html);
}
if (typeof response.swal === 'object') {
$.admin.swal(response.swal);
}
if (typeof response.toastr === 'object') {
$.admin.toastr[response.toastr.type](response.toastr.content, '', response.toastr.options);
}
if (response.then) {
then(response.then);
}
};
var actionCatcherss = function (request) {
$('.modal-footer').show()
$('.tips').remove()
if (request && typeof request.responseJSON === 'object') {
$.admin.toastr.error(request.responseJSON.message, '', {positionClass:"toast-bottom-center", timeOut: 10000}).css("width","500px")
}
};
SCRIPT;
Admin::script($resolve);
return <<<'SCRIPT'
$('.modal-footer').hide()
let html = `导入时间取决于数据量,请耐心等待结果不要关闭窗口!<\/div>`
$('.modal-header').append(html)
process.then(actionResolverss).catch(actionCatcherss);
SCRIPT;
}
}
3、获取 excel 中第一个 文件 sheet 中的信息
round = $round;
}
public function sheets(): array
{
return [
new FirstSheetImport($this->round),
];
}
}
4、获取信息进行导入数据库
round = $round;
}
/**
* @param array $row
*
* @return Model|Model[]|null
*/
public function model(array $row)
{
// 断数据是否
$user = Data::where('mobile', '=', $row['手机'])->first();
if ($user) {
// 存在返回 null
return null;
}
// 数据库对应的字段
return new DataModel([
'name' => $row['姓名'],
'gender' => $row['性别'],
]);
}
public function collection(Collection $rows)
{
//
}
//批量导入1000条
public function batchSize(): int
{
return 1000;
}
//以1000条数据基准切割数据
public function chunkSize(): int
{
return 1000;
}
}
注意需要在models加上
protected $fillable = ['img', 'content','static','username'];
这段来源
Dcat admin
Dcat Admin是一个基于laravel-admin二次开发而成的后台系统构建工具,只需极少的代码即可快速构建出一个功能完善的高颜值后台系统。支持页面一键生成CURD代码,内置丰富的后台常用组件,开箱即用,让开发者告别冗杂的HTML代码,对后端开发者非常友好。
效果图
1. 创建按钮
$grid->tools(function (Grid\Tools $tools) {
// excle 导入
$tools->append(new Reast());
});
2.app/admin/actions/grid 下创建 Reast.php
getKey()}";
// 模态窗
$this->modal($id);
return <<
HTML;
}
protected function modal($id)
{
$form = new Import();
Admin::script('Dcat.onPjaxComplete(function () {
$(".modal-backdrop").remove();
$("body").removeClass("modal-open");
}, true)');
// 通过 Admin::html 方法设置模态窗HTML
Admin::html(
<<
HTML
);
}
/**
* @param Model|Authenticatable|HasPermissions|null $user
*
* @return bool
*/
protected function authorize($user): bool
{
return true;
}
/**
* @return array
*/
protected function parameters()
{
return [];
}
}
3.在Actions/form下创建Import.php
response()
->success('导入成功')
->redirect('/');
//dcat-1.7
//return $this->success('导入成功');
} catch (ValidationException $validationException) {
return Response::withException($validationException);
} catch (Throwable $throwable) {
//dcat 2.0写法
$this->response()->status = false;
return $this->response()->error('上传失败')->refresh();
//dcat 1.7
//return $this->error('上传失败')->refresh();
}
}
public function form()
{
$this->file('file', '上传数据(Excel)')->rules('required', ['required' => '文件不能为空']);
}
}
Buy me a cup of coffee 🙂
觉得对你有帮助,就给我打赏吧,谢谢!