当前位置:   article > 正文

springboot 导入excel表格读取数据转实体类数据插入_读取excel放入实体类

读取excel放入实体类

1.导入pol.xml依赖

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

2.Maper实体类

  1. @Data
  2. @TableName("train.tab_passenger")
  3. public class Passenger implements Serializable {
  4. @ExcelIgnore
  5. @TableId(type = IdType.AUTO)
  6. private Integer id;
  7. @ExcelIgnore
  8. @ApiModelProperty("批次号")
  9. private String batchNo;
  10. @ExcelIgnore
  11. private String departId;
  12. @ExcelIgnore
  13. private String userId;
  14. @ExcelIgnore
  15. private String userName;
  16. @ExcelProperty("姓名")
  17. private String passName;
  18. @ExcelProperty("性别")
  19. private String pasaSex;
  20. @ExcelProperty("联系电话")
  21. private String passTelephone;
  22. @ExcelProperty("证件类型")
  23. private String documentType;
  24. @ExcelProperty("证件号码")
  25. private String documentNumber;
  26. @ExcelProperty("乘客类型")
  27. private String passengerType;
  28. @ExcelIgnore
  29. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  30. private Date createTime;
  31. @ExcelIgnore
  32. private Integer status;
  33. @ExcelIgnore
  34. @TableField(exist = false)
  35. private Integer page;//第几页
  36. @ExcelIgnore
  37. @TableField(exist = false)
  38. private Integer limit;//限制第几条
  39. }

3.PassengerController

  1. /**
  2. * 上传导入乘客excel
  3. * @param file
  4. * @return
  5. */
  6. @PostMapping("/import")
  7. public Result passengerExcel(@RequestParam("file") MultipartFile file) throws IOException {
  8. Result<Object> result = new Result();
  9. //获取文件名
  10. String fileName = file.getOriginalFilename();
  11. //获取文件的后缀名为xlsx
  12. String fileXlsx = fileName.substring(fileName.length()-5);
  13. String fileXls = fileName.substring(fileName.length()-4);
  14. //如果不是excel文件
  15. if(!(fileXlsx.equals(".xlsx") || fileXls.equals(".xls"))) {
  16. result.setCode(400);
  17. result.setMsg("文件格式错误");
  18. return result;
  19. }
  20. //业务操作
  21. try {
  22. List<Passenger> list = EasyExcel.read(file.getInputStream())
  23. .head(Passenger.class)
  24. .sheet()
  25. .doReadSync();
  26. result.setData(list);
  27. return result;
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. result.setCode(500);
  31. result.setMsg(String.valueOf(e));
  32. return result;
  33. }
  34. }

4.返回信息信息

  1. {
  2. "code": 200,
  3. "msg": null,
  4. "data": [
  5. {
  6. "id": null,
  7. "batchNo": null,
  8. "departId": null,
  9. "userId": null,
  10. "userName": null,
  11. "passName": "张三",XXXXX
  12. "pasaSex": "男",
  13. "passTelephone": "19948XXXXX",
  14. "documentType": "JM",
  15. "documentNumber": "53262628200107202612",
  16. "passengerType": "ADULT",
  17. "createTime": null,
  18. "status": null,
  19. "page": null,
  20. "limit": null
  21. }
  22. ]
  23. }

5.图片

 

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

闽ICP备14008679号