使用版本4.1.0
先说一下背景,业务需求根据客户选择,动态导出Excel,根据其数据情况,会有多级表头出现。这种情况下使用注解导出数据就不现实了,模板也不行,我这边使用的是map导出数据(后经过实验,注解导出数据也会出现以下BUG),发现的BUG。
原本4.1.0版本easypoi导出多级表头(三级表头及以上),会有两个BUG,此处以四级表头为例子
一:有多重三级表头情况下,只有最后一个三级表头会有字段名,前面的都为空
如图:
如果你的业务需求只需要一个三级表头,则此BUG不会影响你
二:表头偏移,表头级数越多,更显著
如图:
可能有的小伙伴会说自己也是三级表头,但是没有这个BUG之类的,下面来说这两个BUG的出现原因。
一.空字段
此BUG在4.1.1版本(写文章时的最新版)解决了,但是4.1.1有个新BUG,所以此次修改还是基于4.1.0来处理的。
直接看问题源码
/**
* 创建表头
*/
private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index,
List<ExcelExportEntity> excelParams, int cellIndex) {
Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index);
int rows = getRowNums(excelParams, true);
row.setHeight(title.getHeaderHeight());
Row listRow = null;
if (rows >= 2) {
listRow = sheet.createRow(index + 1);
listRow.setHeight(title.getHeaderHeight());
}
int groupCellLength = 0;
CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor());
}
此处源码是节选easypoi生成表头的源码处,使用换行隔开的就是问题源码所在
因为此BUG官方已经修复了,所以我就不多说了,直接贴上官方修复之后的源码,看一眼就懂BUG出现原因
/**
* 创建表头
*/
private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index,
List<ExcelExportEntity> excelParams, int cellIndex) {
Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index);
int rows = getRowNums(excelParams, true);
row.setHeight((short)title.getHeaderHeight());
Row listRow = null;
if (rows >= 2) {
listRow = sheet.getRow(index + 1);
if (listRow == null) {
listRow = sheet.createRow(index + 1);
listRow.set