前言
这个demo合并了一些其他功能,封装了一个简化easyexcel使用的starter,具体请访问
easyexcel-plus扩展
项目里excel的导出需要一些单元格的合并,但是easyexcel的合并策略比较简单,所以这里就需要自定合并策略
要实现的效果
正文
依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
自定义合并策略
这个是比较简单逻辑,map和cols都要在使用策略时手动拼装进来,
逻辑就是遍历每一行,如果是要合并的起始列,遍历cols要合并的列,合并行。
@Data
@AllArgsConstructor
public class MyMergeStrategy implements RowWriteHandler {
//合并的起始行:key:开始,value;结束
private Map<Integer, Integer> map;
//要合并的列
private List<Integer> cols;
@Override
public void afterRowDispose(RowWriteHandlerContext context) {
//如果是head或者当前行不是合并的起始行,跳过
if (context.getHead() || !map.containsKey(context.getRowIndex())) {
return;
}
Integer endrow = map.get(context.getRowIndex());
if (!context.getRowIndex().equals(endrow)) {
//编列合并的列,合并行
for (Integer col : cols) {
// CellRangeAddress(起始行,结束行,起始列,结束列)
context.getWriteSheetHolder().getSheet().addMergedRegionUnsafe(new CellRangeAddress(context.getRowIndex(), endrow, col, col));
}
}
}
}
测试
public class ExcelTest {
public static void main(String[] args) {
//组装数据
List<FamilyMemberDTO> dtoList = getData();
//组装复杂头
List<List<String>> head = getHead();
List<Object> writeList = new ArrayList<>();
//要合并的行,key:起始行,value:结束行
Map<Integer, Integer> mapMerge = new HashMap<>();
//序号
int count = 1;
//要合并的起始行 从0开始 = head的行数
int index = 3;
for (List<FamilyMemberDTO> dtos : dtoList.stream().collect(Collectors.groupingBy(FamilyMemberDTO::getHzmc)).values()) {
mapMerge.put(index,index+dtos.size()-1);
for (FamilyMemberDTO dto : dtos) {
List<Object> list = new ArrayList<>(7);
Collections.addAll(list, count, dto.getHzmc(), dto.getCyxm(), dto.getCyxb(), dto.getCyzjhm(), dto.getYhzgx(), dto.getLxdh());
writeList.add(list);
}
count++;
index += dtos.size();
}
String fileName = "C:\\Users\\11498\\Desktop\\";
//自定义合并策略 MyMergeStrategy(合并的起始行,要合并的列)
MyMergeStrategy myMergeStrategy = new MyMergeStrategy(mapMerge,Arrays.asList(0,1));
EasyExcel.write(fileName + "test1.xlsx").head(head).registerWriteHandler(myMergeStrategy).sheet().doWrite(writeList);
}
private static List<List<String>> getHead(){
String firstRow = "测试自定义合并";
String familyMember = "家庭成员情况";
List<String> first = Arrays.asList(firstRow, "序号", "序号");
List<String> second = Arrays.asList(firstRow, "户主", "户主");
List<String> third = Arrays.asList(firstRow, familyMember, "成员姓名");
List<String> fourth = Arrays.asList(firstRow, familyMember, "成员性别");
List<String> fifth = Arrays.asList(firstRow, familyMember, "证件号码");
List<String> sixth = Arrays.asList(firstRow, familyMember, "家庭关系");
List<String> seventh = Arrays.asList(firstRow, familyMember, "联系电话");
return Arrays.asList(first, second, third, fourth, fifth, sixth, seventh);
}
private static List<FamilyMemberDTO> getData() {
List<FamilyMemberDTO> list = new ArrayList<>();
FamilyMemberDTO dto = new FamilyMemberDTO();
dto.setHzmc("张三");
dto.setCyxm("张三");
dto.setCyxb("男");
dto.setCyzjhm("123123123123123");
dto.setYhzgx("本人");
dto.setLxdh("123123123123");
FamilyMemberDTO dto2 = new FamilyMemberDTO();
dto2.setHzmc("张三");
dto2.setCyxb("男");
dto2.setCyxm("张小小");
dto2.setCyzjhm("234234234234234234");
dto2.setYhzgx("父子");
dto2.setLxdh("123123123123");
FamilyMemberDTO dto3 = new FamilyMemberDTO();
dto3.setHzmc("张三");
dto3.setCyxb("女");
dto3.setCyxm("张小花");
dto3.setCyzjhm("22343342234234234");
dto3.setYhzgx("父女");
dto3.setLxdh("123123123123");
FamilyMemberDTO dto4 = new FamilyMemberDTO();
dto4.setHzmc("李小小");
dto4.setCyxb("男");
dto4.setCyxm("李小小");
dto4.setCyzjhm("123123123123123");
dto4.setYhzgx("本人");
dto4.setLxdh("123123123123");
FamilyMemberDTO dto5 = new FamilyMemberDTO();
dto5.setHzmc("李小小");
dto5.setCyxb("女");
dto5.setCyxm("韩小小");
dto5.setCyzjhm("234234234234234");
dto5.setYhzgx("夫妻");
dto5.setLxdh("123123123123");
Collections.addAll(list, dto, dto2, dto3, dto4,dto5);
return list;
}
}