ruoyi-cloud excel导入数据
说明
本文以导入部门数据为例,说明excel如何导入
实现
部门页面修改
在ruoyi-ui->src->views->system->dept->index.vue中更改
添加导入按钮
<el-col :span="1.5">
<el-button
type="success"
plain
icon="el-icon-bottom-right"
size="mini"
@click="handleImport"
>导入</el-button>
</el-col>
添加导入弹框
在前添加如下代码
<!-- 部门导入对话框 -->
<el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
<el-upload
ref="upload"
:limit="1"
accept=".xlsx, .xls"
:headers="upload.headers"
:action="upload.url + '?updateSupport=' + upload.updateSupport"
:disabled="upload.isUploading"
:on-progress="handleFileUploadProgress"
:on-success="handleFileSuccess"
:auto-upload="false"
drag
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">
将文件拖到此处,或
<em>点击上传</em>
</div>
<div class="el-upload__tip" slot="tip">
<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的部门信息
</div>
<div class="el-upload__tip" style="color:red" slot="tip">提示:仅允许导入“xls”或“xlsx”格式文件!</div>
</el-upload>
<div slot="footer" class="dialog-footer">
<el-button type="success" @click="importTemplate">下载模板</el-button>
<el-button type="primary" @click="submitFileForm">确 定</el-button>
<el-button @click="upload.open = false">取 消</el-button>
</div>
</el-dialog>
添加对token的引用
import { getToken } from '@/utils/auth'
添加上传遍历
在data()方法的return大括号内添加该变量
//部门导入参数
upload: {
// 是否显示弹出层(用户导入)
open: false,
// 弹出层标题(用户导入)
title: "导入部门基础信息",
// 是否禁用上传
isUploading: false,
// 是否更新已经存在的用户数据
updateSupport: 0,
// 设置上传的请求头部
headers: { Authorization: "Bearer " + getToken() },
// 上传的地址
url: process.env.VUE_APP_BASE_API + "/system/dept/importData"
},
添加相关方法
在methods方法组中添加方法
/* 导入按钮操作 */
handleImport() {
this.upload.title = "部门信息导入";
this.upload.open = true;
},
/** 下载模板操作 */
importTemplate() {
this.download('system/dept/importTemplate', {
}, `部门导入模板.xlsx`)
},
// 文件上传中处理
handleFileUploadProgress(event, file, fileList) {
this.upload.isUploading = true;
},
// 文件上传成功处理
handleFileSuccess(response, file, fileList) {
this.upload.open = false;
this.upload.isUploading = false;
this.$refs.upload.clearFiles();
this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
this.getList();
},
// 提交上传文件
submitFileForm() {
this.$refs.upload.submit();
}
后台代码修改
实体类
需要在SysDept中添加@Excel(name = "标题")让ruoyi获取标题头
/** 部门ID */
@Excel(name = "部门编号")
private Long deptId;
/** 父部门ID */
@Excel(name = "父级部门编号")
private Long parentId;
/** 祖级列表 */
private String ancestors;
/** 部门名称 */
@Excel(name = "部门名称")
private String deptName;
/** 显示顺序 */
@Excel(name = "显示顺序")
private Integer orderNum;
SysDeptController中添加代码
/**
* 部门基础信息列表
*/
@Log(title = "部门管理", businessType = BusinessType.IMPORT)
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
{
ExcelUtil<SysDept> util = new ExcelUtil<>(SysDept.class);
List<SysDept> sysDepts = util.importExcel(file.getInputStream());
String message = deptService.importData(sysDepts, updateSupport);
return AjaxResult.success(message);
}
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response)
{
ExcelUtil<SysDept> util = new ExcelUtil<SysDept>(SysDept.class);
util.importTemplateExcel(response, "部门信息");
}
实现importData方法
在service中添加importData方法,并在impl中实现
/**
* 导入信息
* @param sysDepts 数据列表
* @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
* @return
*/
@Override
public String importData(List<SysDept> sysDepts, boolean isUpdateSupport)
{
if (StringUtils.isNull(sysDepts) || sysDepts.size() == 0) {
throw new ServiceException("导入数据不能为空!");
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
List<SysDept> existList = selectDeptList(new SysDept());
for (SysDept importData : sysDepts) {
try {
//验证是否存在这个信息
boolean userFlag = false;
for (SysDept entry : existList) {
if (entry.getDeptId() == importData.getDeptId()) {
userFlag = true;
break;
}
}
if (!userFlag) {
insertDept(importData);
successNum++;
successMsg.append("<br/>" + successNum + "、数据 " + importData.getDeptName() + " 导入成功");
} else if (isUpdateSupport) {
updateDept(importData);
successNum++;
successMsg.append("<br/>" + successNum + "、数据 " + importData.getDeptName() + " 更新成功");
} else {
failureNum++;
failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getDeptName() + " 已存在");
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + failureNum + "、数据 " + importData.getDeptName() + " 导入失败:";
failureMsg.append(msg + e.getMessage());
System.out.println(msg);
}
}
if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new ServiceException(failureMsg.toString());
} else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
return successMsg.toString();
}
标签:excel,upload,ruoyi,导入,failureNum,importData,new,添加,cloud
From: https://www.cnblogs.com/wanghun315/p/17623022.html