若依分离版实现excel导入功能

若依分离版实现excel导入功能

后端
1.在实体变量上添加@Excel注解,默认导入和导出,若只想导入加入 type = Excel.Type.IMPORT

 /** 负责人 */
    @Excel(name = "责任者")
    private String responsiblePerson;

    /** 封面题名 */
    @Excel(name = "封面题名")
    private String coverTitle;

    /** 起日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "起日期", width = 30, dateFormat = "yyyy-MM-dd")
    private Date startDate;

    /** 止日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "止日期", width = 30, dateFormat = "yyyy-MM-dd")
    private Date endDate;

2.Service层加入方法,List dataManagementList 需更改为自己的实体类

 /**
     * 导入用户数据
     *
     * @param dataManagementList 数据列表
     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importData(List<DataManagement> dataManagementList, Boolean isUpdateSupport, String operName);
    

3.ServiceImpl 实现类 需增加如下 ,红框处皆需要修改

 @Override
    public String importData(List<DataManagement> dataManagementList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(dataManagementList) || dataManagementList.size() == 0)
        {
            throw new CustomException("导入数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<DataManagement> existList = selectDataManagementList(null);
        for (DataManagement importData : dataManagementList)
        {
            try {
      
                boolean userFlag = false;
                for (DataManagement entry : existList) {
                    if (entry.getResponsiblePerson().equals(importData.getResponsiblePerson())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertDataManagement(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getResponsiblePerson() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateDataManagement(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getResponsiblePerson() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getResponsiblePerson() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + importData.getResponsiblePerson() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }

在这里插入图片描述
在这里插入图片描述

4.controller层

 /**
     * 导入数据管理列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<DataManagement> util = new ExcelUtil<DataManagement>(DataManagement.class);
        List<DataManagement> dataManagementList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = iDataManagementService.importData(dataManagementList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<DataManagement> util = new ExcelUtil<DataManagement>(DataManagement.class);
        return util.importTemplateExcel("案卷数据");
    }

在这里插入图片描述

前端:

1 . 在前端index.vue中加入如下 注意:上传地址路径 "/system/user/importData"应改为自己指定路径

// 用户导入参数
upload: {
  // 是否显示弹出层(用户导入)
  open: false,
  // 弹出层标题(用户导入)
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: process.env.VUE_APP_BASE_API + "/system/user/importData"
},

/** 获取token */
import { getToken } from "@/utils/auth";

/** 导入按钮操作 */
handleImport() {
  this.upload.title = "用户导入";
  this.upload.open = true;
},
/** 下载模板操作 */
importTemplate() {
  importTemplate().then(response => {
	this.download(response.msg);
  });
},
// 文件上传中处理
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();
}

2.添加导出按钮事件

<el-button
  type="warning"
  icon="el-icon-download"
  size="mini"
  @click="handleExport"
>导出</el-button>

3.添加导入对话框

<!-- 用户导入对话框 -->
<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" />是否更新已经存在的用户数据
	  <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
	</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="primary" @click="submitFileForm">确 定</el-button>
	<el-button @click="upload.open = false">取 消</el-button>
  </div>
</el-dialog>

4.js文件中添加

// 下载导入模板
export function importTemplate() {
  return request({
    url: '/system/dataManagement/importTemplate',
    method: 'get'
  })
}

完成导入功能

若依官方文档

csdn博主
https://blog.csdn.net/hutu1234567/article/details/107715319?utm_source=app&app_version=4.5.8

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值