当前位置:   article > 正文

springboot + vue + easyexcel 实现简单的excel数据导入_@exceldictformat

@exceldictformat

1、引入 easyexcel依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>${easyexcel.version}</version>
  5. </dependency>

SubjectVo实体类

  1. package com.ruoyi.subject.domain.vo;
  2. import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
  3. import com.alibaba.excel.annotation.ExcelProperty;
  4. import com.ruoyi.common.annotation.ExcelDictFormat;
  5. import com.ruoyi.common.convert.ExcelDictConvert;
  6. import io.swagger.annotations.ApiModel;
  7. import io.swagger.annotations.ApiModelProperty;
  8. import lombok.AllArgsConstructor;
  9. import lombok.Data;
  10. import lombok.NoArgsConstructor;
  11. /**
  12. * 学科视图对象 tb_subject
  13. *
  14. * @author ruoyi
  15. * @date 2023-09-22
  16. */
  17. @Data
  18. @AllArgsConstructor
  19. @NoArgsConstructor
  20. @ExcelIgnoreUnannotated
  21. @ApiModel(value="学科 vo对象 ", description="学科vo对象 tb_subject")
  22. public class SubjectVo {
  23. private static final long serialVersionUID = 1L;
  24. /**
  25. * 主键
  26. */
  27. @ApiModelProperty(value = "主键")
  28. private Long id;
  29. /**
  30. * 排序
  31. */
  32. @ApiModelProperty(value = "排序")
  33. @ExcelProperty(value = "排序")
  34. private Long sort;
  35. /**
  36. * 门类
  37. */
  38. @ExcelProperty(value = "门类", converter = ExcelDictConvert.class)
  39. @ExcelDictFormat(dictType = "subject_category")
  40. @ApiModelProperty(value = "门类")
  41. private String category;
  42. /**
  43. * 专业类
  44. */
  45. @ExcelProperty(value = "专业类", converter = ExcelDictConvert.class)
  46. @ExcelDictFormat(dictType = "speciality_type")
  47. @ApiModelProperty(value = "专业类")
  48. private String specialityType;
  49. /**
  50. * 上级学科代码
  51. */
  52. @ApiModelProperty(value = "上级学科代码")
  53. private String parentCode;
  54. /**
  55. * 学科代码
  56. */
  57. @ApiModelProperty(value = "学科代码")
  58. @ExcelProperty(value = "学科代码")
  59. private String code;
  60. /**
  61. * 学科名称
  62. */
  63. @ApiModelProperty(value = "学科名称")
  64. @ExcelProperty(value = "学科名称")
  65. private String name;
  66. /**
  67. * 学位授予门类
  68. */
  69. @ExcelProperty(value = "学位授予门类", converter = ExcelDictConvert.class)
  70. @ExcelDictFormat(dictType = "subject_category")
  71. @ApiModelProperty(value = "学位授予门类")
  72. private String degreeGrantingType;
  73. /**
  74. * 修业年限
  75. */
  76. @ApiModelProperty(value = "修业年限")
  77. @ExcelProperty(value = "修业年限")
  78. private String schoolingYears;
  79. /**
  80. * 增设年份
  81. */
  82. @ApiModelProperty(value = "增设年份")
  83. @ExcelProperty(value = "增设年份")
  84. private String addYear;
  85. }

注意:导入的表格中所需要的字段在实体类中必须加上 @ExcelProperty()

2、service层

List<SubjectVo> importData(MultipartFile file)

3、serviceImpl层

  1. //数据导入
  2. @Override
  3. public List<SubjectVo> importData(MultipartFile file) throws IOException {
  4. List<SubjectVo> subjectVoList = EasyExcel.read(file.getInputStream())
  5. .head(SubjectVo.class)
  6. .sheet()
  7. .doReadSync();
  8. log.info("导入数据:{}",subjectVoList);
  9. if (null != subjectVoList){
  10. for (SubjectVo vo : subjectVoList) {
  11. Subject subject = new Subject();
  12. BeanUtils.copyProperties(vo,subject);//对象转换
  13. subject.setId(System.currentTimeMillis());
  14. subject.setDelFlag("0");
  15. baseMapper.insert(subject);
  16. }
  17. }
  18. return subjectVoList;
  19. }

4、controller层

  1. //数据导入
  2. @PostMapping("/importData")
  3. @ApiOperation("数据导入")
  4. public R<?> importData(@RequestPart("file") MultipartFile file) throws IOException {
  5. return R.ok(iSubjectService.importData(file));
  6. }

5、使用Apifox工具进行测试

 6、vue前端页面

  1. <el-upload
  2. class="upload-demo"
  3. action="http://localhost:8080/subject/subject/importData"
  4. :show-file-list="false"
  5. :on-success="successCallback"
  6. :on-progress="progress"
  7. >
  8. <el-button size="small" type="primary">数据导入</el-button>
  9. </el-upload>

action为后端controller的接口,由于这种方式没设置token,所以,如果采用这种简单方法,需要将请求接口加入白名单放行。

其中:

:on-success是文件上传成功时调用的函数

:on-progress是文件上传时调用的函数

:show-file-list是  是否显示已上传文件列表

successCallback函数:

  1. successCallback(res,file,fileList){
  2. if (res.code == 200){
  3. this.$loading().close()
  4. this.$message.success("数据导入成功!")
  5. this.getList();
  6. }
  7. },

progress函数:

  1. progress(event,file,fileList){
  2. this.$loading({
  3. lock: true,
  4. text: "正在导入数据,请稍等...",
  5. spinner: 'el-icon-loading',
  6. background: 'rgba(0,0,0,0.5)'
  7. })
  8. },

7、前端上传效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/283730
推荐阅读
相关标签
  

闽ICP备14008679号