当前位置:   article > 正文

SpringBoot笔记 | EasyExcel导入导出及基于模板导出_easyexcel模板导出

easyexcel模板导出

目录

开发准备

导出

常用注解

导出excel到指定位置

导出excel到指定web

导入

将指定位置Excel导入并显示至web

 使用ExcelWriter基于模板导出


开发准备

1.导入依赖

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

实体类

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Student {
  5. //生成表格时,次字段不生成
  6. @ExcelIgnore
  7. private String id;
  8. //定义表头名称及位置,value表示列名,0表示第一列
  9. @ExcelProperty(value = "姓名",index = 0)
  10. private String name;
  11. @ExcelProperty(value = "年龄",index = 1)
  12. private String age;
  13. @ColumnWidth(20)
  14. @ExcelProperty(value = "生日",index = 2)
  15. private String birthday;
  16. }

数据源

  1. @Component
  2. public class StudentMapper {
  3. public List<Student> getStudents(){
  4. List<Student> studentList = new ArrayList<>();
  5. studentList.add(new Student("1","小明","16","1997-03-02"));
  6. studentList.add(new Student("2","小红","17","1993-03-02"));
  7. studentList.add(new Student("3","小东","18","1994-03-02"));
  8. return studentList;
  9. }
  10. }

导出

常用注解

@ExcelProperty 列名,通过index属性指定位置

@ExcelIgnore 忽略该字段不进行导出

@DateTimeFormat 日期格式转换,String接收excel日期使用

@NumberFormat  数字格式转换 String接收excel数字格式使用

导出excel到指定位置

  1. public static void export2File(String path, String excelName, String sheetName, Class clazz, List data){
  2. String fileName = path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue());
  3. EasyExcel.write(fileName,clazz).sheet(sheetName).doWrite(data);
  4. }

参数说明:

export2File(path,"学生表","学生信息", Student.class,studentMapper.getStudents());

path:导出位置 如:"D:\\test11\\"

excelName:导出表格名字

sheetName:第一个sheet名字

clazz:导出数据对应的实体类

List data:导出数据源

导出excel到指定web

  1. public static void export2Web(HttpServletResponse response,String excelName, String sheetName,Class clazz,List data) throws Exception{
  2. response.setContentType("application/vnd.ms-excel");
  3. response.setCharacterEncoding("utf-8");
  4. //URLEncoder.encoder防止中文乱码
  5. excelName = URLEncoder.encode(excelName,"utf-8");
  6. response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
  7. EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
  8. }

导入

将指定位置Excel导入并显示至web

  1. public static String export2Web4File(HttpServletResponse response, String path, String excelName) throws UnsupportedEncodingException {
  2. File file = new File(path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue()));
  3. if (!file.exists()) {
  4. return "文件不存在!";
  5. }
  6. response.setContentType("application/vnd.ms-excel");
  7. response.setCharacterEncoding("utf-8");
  8. // 这里URLEncoder.encode可以防止中文乱码
  9. excelName = URLEncoder.encode(excelName, "UTF-8");
  10. response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
  11. try (
  12. FileInputStream in = new FileInputStream(file);
  13. ServletOutputStream out = response.getOutputStream();
  14. ) {
  15. IOUtils.copy(in, out);
  16. return "导出成功!";
  17. } catch (Exception e) {
  18. log.error("导出文件异常:", e);
  19. }
  20. return "导出失败!";
  21. }

 使用ExcelWriter基于模板导出

  1. private void export(List<DataVO> dataList) {
  2. ExcelWriter excelWriter = null;
  3. //获取项目resources/template目录下模板的数据流
  4. InputStream templateInputStream = null;
  5. //构建第一个sheet页的数据,根据模板填充
  6. Map<String, Object> sheetMap = new HashMap<>();
  7. //与模板对应字段-值设置
  8. sheetMap.put("data1", "测试数据1");
  9. sheetMap.put("data2", "测试数据2");
  10. sheetMap.put("data3", "测试数据3");
  11. String filePath = null;
  12. try {
  13. //获取项目resources/template目录下模板的数据流
  14. templateInputStream = new
  15. ClassPathResource("template/data_template.xlsx").getInputStream();
  16. if (templateInputStream.available() == 0) {
  17. log.error("获取模板失败");
  18. }
  19. //设置文件名,filePath导出路径: 暂存java临时目录 最后再删除 "java.io.tmpdir"
  20. String fileName = "测试报告" + System.currentTimeMillis() + ".xlsx";
  21. String path = System.getProperty("java.io.tmpdir") + File.separator;
  22. String filePath = path + fileName;
  23. //ExcelWriter通过POI将值写入Excel
  24. excelWriter = EasyExcel.write(filePath).withTemplate(templateInputStream).build();
  25. // sheet-概览
  26. WriteSheet firstSheet = EasyExcel.writerSheet(0).build();
  27. //用数据去填充模板 取对应的值显示在模板对应的位置
  28. excelWriter.fill(sheetMap, firstSheet);
  29. //如果有第二页sheet 非模板 直接生成excel
  30. WriteSheet secondSheet = EasyExcel.writerSheet(1).head(DataVO.class).build();
  31. excelWriter.write(dataList, secondSheet );
  32. } catch (IOException e) {
  33. log.error("获取模板:examreport 失败。");
  34. e.printStackTrace();
  35. } finally {
  36. //最后记得关闭流
  37. excelWriter.finish();
  38. IOUtils.closeQuietly(templateInputStream);
  39. //删除临时文件
  40. if (null != filePath) {
  41. ExcelUtils.deleteFile(filePath);
  42. }
  43. }
  44. }
  45. @Data
  46. @NoArgsConstructor
  47. @AllArgsConstructor
  48. public class DataVO{
  49. @ExcelProperty(value = "姓名")
  50. @ColumnWidth(value = 15)
  51. private String name;
  52. @ExcelProperty(value = "年龄")
  53. @ColumnWidth(value = 15)
  54. private String age;
  55. }

模板文件data_template.xlsx 

用{}表示占位,数据根据map键值填充

 

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

闽ICP备14008679号