当前位置:   article > 正文

SpringBoot集成EasyExcel实现excel的导入导出_springboot easyexcel导入

springboot easyexcel导入

在我们做的系统中,经常需要通过excel文件导入添加到我们的数据库,并需要将数据库的列表进行导出保存到本地,那么,SpringBoot应该如何操作呢?

首先EasyExcel官网地址:EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel

前情提要:本例子的前端用的是vue、element-ui

1.导入依赖

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

2.将实体类与表进行规范

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @TableName("sys_user")
  5. public class User {
  6. @TableId(value = "id",type = IdType.AUTO )
  7. @ExcelProperty("编号")
  8. private Integer id;
  9. @ExcelProperty("用户名")
  10. private String username;
  11. @JsonIgnore //不展示密码字段
  12. private String password;
  13. @ExcelProperty("昵称")
  14. private String nickname;
  15. @ExcelProperty("邮箱")
  16. private String email;
  17. @ExcelProperty("电话")
  18. private String phone;
  19. @ExcelProperty("地址")
  20. private String address;
  21. @ExcelProperty("头像")
  22. @TableField("avatar_url")
  23. private String avatarUrl;
  24. }

 ExcelProperty意思相信大家也看得出来,就是将实体类的变量与excel表格的列对应起来。容易理解。

接下来就是写接口了

3.导入

  1. /**
  2. * 导入的excel文件对象是MultipartFile类型的
  3. * @param file
  4. * @throws IOException
  5. */
  6. @PostMapping("/import")
  7. public void ExcelImport(MultipartFile file)throws IOException{
  8. InputStream is = file.getInputStream();
  9. UserReadListener userReadListener = new UserReadListener(userMapper);
  10. EasyExcel.read(is,User.class,userReadListener)
  11. .sheet(0)
  12. .headRowNumber(1)
  13. .doRead();
  14. }

 UserReadListener是一个关键的地方,这里其实就是导入的对象监听器

  1. import com.alibaba.excel.context.AnalysisContext;
  2. import com.alibaba.excel.read.listener.ReadListener;
  3. import com.cbz.springboot.mapper.UserMapper;
  4. public class UserReadListener implements ReadListener<User> {
  5. private UserMapper userMapper;
  6. public UserReadListener(UserMapper userMapper){
  7. this.userMapper = userMapper;
  8. }
  9. @Override
  10. public void invoke(User user, AnalysisContext analysisContext) {
  11. System.out.println("读取到:"+user);
  12. userMapper.insert(user);
  13. }
  14. @Override
  15. public void doAfterAllAnalysed(AnalysisContext analysisContext) {
  16. System.out.println("读取完毕!!!");
  17. }
  18. }

实现ReadListener类的2个方法,invoke就是文件的每一行(除了列名)都会执行一下,所以我们就需要添加到我们的数据库中!doAfterAllAnalysed就是文件的所有内容读取完的时候就会执行一下这个函数。

那么这里为什么不是用Autowired注解来注入UserMapper呢?

答:因为ReadListener不是spring的一个组件,不能进行自动装配!

4.导出

  1. //导出
  2. @GetMapping("/export")
  3. public void ExcelExport(HttpServletResponse response)throws IOException {
  4. List<User> users = userMapper.selectList(null);
  5. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");//.xlsx格式
  6. response.setCharacterEncoding("utf-8");
  7. String fileName = URLEncoder.encode("用户数据", "UTF-8").replaceAll("\\+", "%20");
  8. response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
  9. EasyExcel.write(response.getOutputStream())
  10. .head(User.class)
  11. .excelType(ExcelTypeEnum.XLSX)
  12. .sheet("用户数据")
  13. .doWrite(users);
  14. }

.sheet("用户数据")意思就是如下


URLEncoder.encode("用户数据", "UTF-8")意思就是如下

 

 第一句:

List<User> users = userMapper.selectList(null);

就是把数据库的内容全部查找出来

接下来再写到excel里面,然后再导出!

~~~~~~~~~~~~~~~~~后端到这里结束了,我们看看前端

  1. <el-upload
  2. action="http://localhost:9090/user/import" :show-file-list="false" accept=".xlsx"
  3. :on-success="handleImportSuccess"
  4. style="display: inline-block;margin-right: 10px;">
  5. <el-button type="primary">导入 <i class="el-icon-bottom"></i></el-button>
  6. </el-upload>
  7. <el-button type="primary" @click="exportExcel">导出 <i class="el-icon-top"></i></el-button>
  8. </div>

 

  1. exportExcel(){
  2. window.open("http://localhost:9090/user/export")
  3. },
  4. handleImportSuccess(){
  5. this.$message.success("导入成功!")
  6. this.load()
  7. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号