功能展示
功能类似这样:
开题序言
当时网上的资料大部分都是上下行相同的数据合并,唯独没有左右列的单元格合并。因此找资料花了一点时间,但功夫不负有心人,终于看到有两个和我要的功能差不多类似的文章。需要自取:
EasyExcel模板导出(行和列自动合并)_Lzfnemo2009的博客-CSDN博客_easyexcel模板导出
EasyExcel导出自定义合并单元格 策略 个人理解
写这篇文章只是为了自己以后参考。
以下进入正文:
1、导出的实体类,也就是表头
@Data
public class CityCapacityPo {
@ExcelProperty(value = "时间",index = 0)
private String time;
@ExcelProperty(value = "出口",index = 1)
private String export;
@ExcelProperty(value = "地市",index = 2)
private String direction;
@ExcelProperty(value = "数据1",index = 3)
private Double data1;
@ExcelProperty(value = "数据2",index = 4)
private Double data2;
@ExcelProperty(value = "数据3",index = 5)
private Double data3;
@ExcelProperty(value = "数据4",index = 6)
private Double data4;
@ExcelProperty(value = "数据5",index = 7)
private Double data5;
@ExcelProperty(value = "数据6",index = 8)
private Double data6;
}
2、行合并工具类
public class ExcelFillCellMergeStrategyUtils implements CellWriteHandler {
/**
* 需要合并列的下标,从0开始
*/
private int[] mergeColumnIndex;
/**
* 从第几行开始合并,表头下标为0
*/
private int mergeRowIndex;
public ExcelFillCellMergeStrategyUtils(int mergeRowIndex, int[] mergeColumnIndex) {
this.mergeRowIndex = mergeRowIndex;
this.mergeColumnIndex = mergeColumnIndex;
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
//当前行
int curRowIndex = cell.getRowIndex();
//当前列
int curColIndex = cell.getColumnIndex();
if (curRowIndex > mergeRowIndex) {
for (int i = 0; i mergeRegions = sheet.getMergedRegions();
boolean isMerged = false;
for (int i = 0; i
3、列合并的工具类
@Data @AllArgsConstructor public class CellLineRange { /** * 起始列 */ private int firstCol; /** * 结束列 */ private int lastCol; }
public class ExcelFillCelMergeStrategy implements CellWriteHandler {
//自定义合并单元格的列 如果想合并 第4列和第5例 、第6列和第7例: [CellLineRange(firstCol=3, lastCol=4), CellLineRange(firstCol=5, lastCol=6)]
private List cellLineRangeList;
//自定义合并单元格的开始的行 一般来说填表头行高0 表示从表头下每列开始合并 :如表头行高位为3则 int mergeRowIndex = 2 ;
private int mergeRowIndex;
public ExcelFillCelMergeStrategy(List cellLineRangeList, int mergeRowIndex) {
this.cellLineRangeList=cellLineRangeList;
this.mergeRowIndex=mergeRowIndex;
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
//当前单元格的行数
int curRowIndex = cell.getRowIndex();
// 当前单元格的列数
int curColIndex = cell.getColumnIndex();
if (curRowIndex > mergeRowIndex) {
if (curRowIndex > mergeRowIndex) {
for (int i = 0; i cellLineRangeList.get(i).getFirstCol()&&curColIndex mergeRegions = sheet.getMergedRegions();
//是否合并
boolean isMerged = false;
for (int i = 0; i
4、调用工具类,开始合并:
@PostMapping("/exportCityOutletCapacityList")
@ApiOperation("导出Excel表")
public void exportCityOutletCapacityList(@RequestBody OutletCapacityParam param, HttpServletResponse response) {
//获取需要导出的表数据
List list=capacityFlowDao.selectCityOutletCapacityList(param);
try {
String fileName = "测试合并单元格";
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
CityCapacityPo capacityPo=new CityCapacityPo();
capacityPo.setExport("汇总");
capacityPo.setTime("汇总");
capacityPo.setDirection("汇总");
//遍历列表,求各数据汇总
capacityPo.setData1(list.stream().filter(Po-> Po.getData1()!=null).mapToDouble(CityCapacityPo::getData1).sum());
capacityPo.setData2(list.stream().filter(Po-> Po.getData2()!=null).mapToDouble(CityCapacityPo::getData2).sum());
capacityPo.setData3(list.stream().filter(Po-> Po.getData3()!=null).mapToDouble(CityCapacityPo::getData3).sum());
capacityPo.setData4(list.stream().filter(Po-> Po.getData4()!=null).mapToDouble(CityCapacityPo::getData4).sum());
capacityPo.setData5(list.stream().filter(Po-> Po.getData5()!=null).mapToDouble(CityCapacityPo::getData5).sum());
capacityPo.setData6(list.stream().filter(Po-> Po.getData6()!=null).mapToDouble(CityCapacityPo::getData6).sum());
list.add(capacityPo);
ArrayList cellLineRanges=new ArrayList();
//设置第几列开始合并
int[] mergeColumnIndex = {0, 1};
//设置第几行开始合并
int mergeRowIndex = 1;
cellLineRanges.add(new CellLineRange(0,2));
EasyExcel.write(response.getOutputStream())
//设置行合并单元格
.registerWriteHandler(new ExcelFillCellMergeStrategyUtils(mergeRowIndex,mergeColumnIndex))
//设置行合并单元格
.registerWriteHandler(new ExcelFillCelMergeStrategy(cellLineRanges,list.size()-1))
.head(CityCapacityPo.class)
.sheet("sheet1")
.doWrite(list);
} catch (Exception e) {
e.printStackTrace();
}
}
以上就是行和列的合并过程了,主要的内容就是行和列合并的工具类,网上的资料都差不多。其实很多我确实也没参透,只是依样画葫芦,碰巧实现了。