EasyExcel 写入(导出)

互联网的精髓就是共享,可以共享技术、共享经验、共享情感、共享快乐~

很多年前就有这个想法了,从事IT行业时间也不短了,应该把自己工作和业余所学习的东西记录并分享出来,和有缘人一起学习和交流。

如果您是那个有缘人,请上岛一叙!爪哇岛随时欢迎您!


今天,咱们一起来看看使用EasyExcel做Excel的导出(数据写入到Excel中)。。。。。。

EasyExcel导出Excel在官网(EasyExcel官网-导出数据)上面已经有很多基础的例子,这些我再重复摘抄一遍就没有啥意义了,这部分大家可以根据自己的实际场景自己去官网查找例子代码即可;

本篇文章我主要分享的是:如何通过反射机制封装一些较为复杂的动态场景下的工具类,使用工具类来帮我们减少重复劳动,让生成报表变得很简单。

功能列表:

  1. 最简单的Excel导出
  2. 通过registerWriteHandler控制单元格样式
  3. 通过head自定义表头
  4. 合并表头(静态)
  5. 动态表头(动态合并表头 & 横向无限扩展列)
  6. 多sheet页

代码片段

@Data
public class Student {
@ExcelIgnore
private String stuId; @ExcelProperty("姓名", index=0)
private String name;
@ExcelProperty("学科", index=1)
private String subject;
@ExcelProperty("分数", index=2)
private Double score;
} public Object exportExcel(Class <? > excelBeanClass, List < List <? >> dataList, String title) {
File file = new File(CommonConstants.CDN_FILE_LOCAL_REPORT);
boolean mkdir = true;
if(!file.exists()) {
mkdir = file.mkdirs();
}
if(!mkdir) {
return new ASOError(CommonConstants.ErrorEnum.OPRATION_FAIL.getCode(), "创建文件失败");
}
String fileName = title + "_" + System.currentTimeMillis() + ".xlsx";
String filePath = CommonConstants.CDN_FILE_LOCAL_REPORT + File.separator + fileName; // 核心
witeExcel(filePath, title, excelBeanClass, dataList); ObjectResponse < String > resp = new ObjectResponse < > ();
String downloadPath = CommonConstants.CDN_FILE_REMOTE + "report" + File.separator + fileName;
resp.setData(downloadPath);
logger.info("generateReport: downloadPath={}", downloadPath);
return resp;
}
  1. 最简单的Excel导出
public void witeExcel(String file, String title, Class <? > excelBeanClass, List < List <? >> dataList) {
ExcelWriterBuilder write = EasyExcel.write(filePath, excelBeanClass);
write.autoCloseStream(true).sheet(title).doWrite(dataList);
} public static void main(String []args) {
List < List <? >> data = new ArrayList();
......
System.out.println("download url is :" + exportExcel(Student.class, data, "Excel名称"));
}
  1. 通过registerWriteHandler控制单元格样式
public void witeExcel(String file, String title, Class <?> excelBeanClass, List <List<?>> dataList, List<WriteHandler> writeHandlerList) {
ExcelWriterBuilder write = EasyExcel.write(filePath, excelBeanClass);
if(CollectionUtils.isNotEmpty(writeHandlerList)) {
**writeHandlerList.forEach(write::registerWriteHandler);**
}
write.autoCloseStream(true).sheet(title).doWrite(dataList);
} public static void main(String []args) {
//获取头和内容的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteCellStyle.setWriteFont(headWriteFont);
// ...内容单元格也可以同样方式设置样式...
WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); //列宽的策略,宽度是小单位
Integer columnWidthArr[] = {3000, 3000, 2000, 6000};
List<Integer> columnWidths = Arrays.asList(columnWidthArr);
CustomSheetWriteHandler customSheetWriteHandler = new CustomSheetWriteHandler(columnWidths); //自定义单元格策略
CustomCellWriteHandler = new CustomCellWriteHandler(yellowRowsSet); List<WriteHandler> writeHandlerList = new ArrayList();
writeHandlerList.add(horizontalCellStyleStrategy);
writeHandlerList.add(customSheetWriteHandler);
writeHandlerList.add(customCellWriteHandler); List<List<Student>> data = new ArrayList();
......
System.out.println("download url is :" + exportExcel(Student.class, data, "Excel名称"));
}
  1. 通过head自定义表头
public void witeExcel(String file, String title, List<List<String>> head, List < List <? >> dataList) {
ExcelWriterBuilder = EasyExcel.write(filePath);
// head可以根据需要自定义
**write.head(head);**
write.autoCloseStream(true).sheet(title).doWrite(dataList);
} public static void main(String []args) {
List < List <? >> data = new ArrayList();
......
System.out.println("download url is :" + exportExcel(Student.class, data, "Excel名称"));
}
  1. 合并表头(静态)
@Data
public class Student {
@ExcelIgnore
private String stuId; @ExcelProperty(value="姓名", index=0)
private String name;
@ExcelProperty(value="成绩, 学科", index=1)
private String subject;
@ExcelProperty(value="成绩, 分数", index=2)
private Double score;
}
姓名 成绩
学科 分数
AAA 语文 100
  1. 动态表头(动态合并表头 & 横向无限扩展列)
//基于Student对象的配置完成表头合并(静态)
public void witeExcel(String mainTitle, Class <?> excelBeanClass) {
Field[] fields = excelBeanClass.getDeclaredFields();
for(Field field: fields) {
ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelProperty.class);
if(excelProperty != null) {//打了ExcelProperty注解的字段处理
try {
InvocationHandler excelH = Proxy.getInvocationHandler(excelProperty);
Field excelF = excelH.getClass().getDeclaredField("memberValues");
excelF.setAccessible(true);
Map < String, Object > excelPropertyValues = (Map<String, Object>) excelF.get(excelH);
excelPropertyValues.put("value", new String[] {mainTitle, excelProperty.value()[0]});
} catch(Exception e) {
//TODO:异常处理
}
}
}
} //基于Student对象对表头进行动态合并(动态)
public void dynamicHeaderByExcelProperty(String mainTitle, String secondTitle, Class <? > excelBeanClass) {
Field[] fields = excelBeanClass.getDeclaredFields();
for(Field field: fields) {
ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelProperty.class);
if(excelProperty != null) {
try {
InvocationHandler excelH = Proxy.getInvocationHandler(excelProperty);
Field excelF = excelH.getClass().getDeclaredField("memberValues");
excelF.setAccessible(true);
Map <String, Object> excelPropertyValues = (Map <String, Object> ) excelF.get(excelH);
excelPropertyValues.put("value", new String[] { mainTitle, secondTitle, excelProperty.value()[0]});
} catch(Exception e) {
//TODO:异常处理
}
}
}
} //自定义表头的单级动态合并及横向无限扩展列
public List<List<String>> dynamicHeaderByCustom(String mainTitle, Class <?> excelBeanClass) {
List<List<String>> headList = new ArrayList<>();
Field[] fields = excelBeanClass.getDeclaredFields();
for(Field field: fields) {
ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelProperty.class);
if(excelProperty != null) {
List <String> fieldHeadList = new ArrayList<> ();
// TODO:待优化扩展,指定不同列的合并
if(StringUtils.isNotBlank(mainTitle)) {
fieldHeadList.add(mainTitle);
}
fieldHeadList.addAll(Arrays.asList(excelProperty.value()));
headList.add(fieldHeadList);
}
}
return headList;
}
//自定义表头的多级动态合并及横向无限扩展列
public List<List<String>> dynamicHeaderByCustom(String mainTitle, List <Class<?>> excelBeanClassList) {
List<List<String>> headList = new ArrayList<>();
System.out.println("excelBeanClassList.size()=" + excelBeanClassList.size());
excelBeanClassList.forEach(v - > {
headList.addAll(this.dynamicHeaderByCustom(mainTitle, v));
});
return headList;
} public static void main(String[] args) {
List < List <? >> data = new ArrayList();
......
System.out.println("download url is :" + exportExcel("成绩", "科目", data, "Excel名称"));
}
姓名 成绩 A卷(动态) B卷(动态)
学科 分数 填空题 判断题 问答题 附加题 填空题 判断题 问答题 附加题
AAA 数学 110 20分 15分 45分 10分 20分 20分 50分 10分
  1. 多sheet页
public void witeExcel(Class<?> excelBeanClass, String title, List<ReportSheetInfo> sheets) {
ExcelWriterBuilder write;
if(excelBeanClass != null) {
write = EasyExcel.write(filePath, targetClass);
} else {
write = EasyExcel.write(filePath);
}
ExcelWriter excelWriter = write.build();
sheets.forEach(v - > {
ExcelWriterSheetBuilder writeSheet = EasyExcel.writerSheet(sheets.indexOf(v), v.getSheetTitle());
writeSheet.head(v.getHead());
if(CollectionUtils.isNotEmpty(v.getWriteHandlerList())) {
v.getWriteHandlerList().forEach(writeSheet::registerWriteHandler);
}
excelWriter.write(v.getDataList(), writeSheet.build());
});
excelWriter.finish();
write.autoCloseStream(true);
} // ReportSheetInfo类中定义writeHandlerList、dataList、head集合
public static void main(String[] args) {
List<ReportSheetInfo> sheets = new ArrayList();
......
System.out.println("download url is :" + exportExcel(Student.class, "Excel名称", sheets));
}

以上是关于EasyExcel的导出Excel封装,部分源码已贴出,需要完整源码的可以在下方评论留言,今天分享就到这里。。。。。。

海量数据Excel报表利器——EasyExcel(一 利用反射机制导出Excel)的更多相关文章

  1. 海量数据Excel报表利器——EasyExcel(开场篇)

    EasyExcel 简介篇 互联网的精髓就是共享,可以共享技术.共享经验.共享情感.共享快乐~ 很多年前就有这个想法了,从事IT行业时间也不短了,应该把自己工作和业余所学习的东西记录并分享出来,和有缘 ...

  2. Java基于注解和反射导入导出Excel

    代码地址如下:http://www.demodashi.com/demo/11995.html 1. 构建项目 使用Spring Boot快速构建一个Web工程,并导入与操作Excel相关的POI包以 ...

  3. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  4. android 利用反射机制获取drawable中所有的图片资源

    public List<Map<String,Object>> getGridData() { list=new ArrayList<Map<String,Obje ...

  5. 【转】Java利用反射机制访问私有化构造器

    Java利用反射机制访问私有化构造器 博客分类: java   我们都知道,当一个类的构造方法被设为私有的时候(private),在其他类中是无法用new来实例化一个对象的. 但是有一种方法可以把带有 ...

  6. Android利用反射机制为实体类属性赋值

    在做android项目时,有时会遇到从网络上获取json类型数据,赋值给实体类,实体类属性少可以一个一个的赋值,如果实体类有很多属性,赋值可能就要耗很长的功夫了,幸好Java给我们提供了反射机制.下面 ...

  7. java利用反射机制判断对象的属性是否为空以及获取和设置该属性的值

    1.java利用反射机制判断对象的属性是否为空: Map<String,String> validateMap = new LinkedHashMap<String, String& ...

  8. C# 中利用反射机制拷贝类的字段和属性(拷贝一个类对象的所有东西付给另一个类对象,而不是付给引用地址)

    from:https://blog.csdn.net/poxiaohai2011/article/details/27555951 //C# 中利用反射机制拷贝类的字段和属性(拷贝一个类对象的所有东西 ...

  9. java 中利用反射机制获取和设置实体类的属性值

    摘要: 在java编程中,我们经常不知道传入自己方法中的实体类中到底有哪些方法,或者,我们需要根据用户传入的不同的属性来给对象设置不同的属性值,那么,java自带的反射机制可以很方便的达到这种目的,同 ...

随机推荐

  1. inux软件安装管理之——dpkg与apt-*详解

    inux软件安装管理之--dpkg与apt-*详解 Nosee123关注 0.5922017.09.12 17:47:44字数 3,894阅读 8,565 [Linux软件安装管理系列]- - 传送门 ...

  2. mysql基础之mariadb概念

    一.数据库介绍 什么是数据库(Database)? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库 ...

  3. 存储单位 KB MB bit

  4. 在Linux服务器,搭建K8s服务【脚本篇】

    前言 好久没有写博客了,本文主要是对网上文章的总结篇,主要是将安装和运行代码做了一次真机实验,亲测可用.文章内包含的脚本和代码,多来自于网络,也有我自己的调整和配置,文章末尾对参考的文献做了列举,方便 ...

  5. l初识CSRF(跨站请求伪造)

    一 CSRF是什么 CSRF(Cross-site request forgery)跨站请求伪造,也被称为"One Click Attack"或者Session Riding,通常 ...

  6. VMware vSphere 7.0 Update 2 发布 - 数据中心虚拟化和 Kubernetes 云原生应用引擎

    2021 年 3 月 9 日,VMware 发布了 vSphere 7 Update 2.它可以通过 VMware Customer Connect 和 vSphere Lifecycle Manag ...

  7. 对狂神说java的springboot中spring security的总结

    1.spring security的环境搭建 首先新建一个springboot项目,只够选web中的spring web依赖 然后在pom.xml导入相关依赖 <!--thymeleaf模块-- ...

  8. javascript数组排序算法之选择排序

    前言 作为一名程序员数组的排序算法是必须要掌握的,今天来图解----选择排序 选择排序原理 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大) ...

  9. CUDA 11功能清单

    CUDA 11功能清单 基于NVIDIA Ampere GPU架构的新型NVIDIA A100 GPU在加速计算方面实现了最大的飞跃.A100 GPU具有革命性的硬件功能,CUDA 11与A100一起 ...

  10. HAL库|神器cubemx的正确打开方式

    前言 工欲善其事,必先利其器.HAL库的开发不一定必须使用cubemx,但是使用了cubemx,你绝对不会后悔.基于一些小伙伴对cubemx的使用还有一些疑问,本次小飞哥从新建工程到生成工程,编写应用 ...