普通导出数据
需求
需要将相同列数据合并
效果图:
代码:
package cn.silence.test;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* 班级信息
*/
@Data
@AllArgsConstructor
public class ClassInfo {
/**
* 学院
*/
private String academy;
/**
* 系
*/
private String system;
/**
* 专业
*/
private String specialized;
/**
* 班级
*/
private String className;
}
package cn.silence.test;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;
public class Excel {
public static void main(String[] args) {
//读取数据 并根据需求排序
TreeSet<ClassInfo> set = getClassInfos();
//excel写出位置
File file = new File("C:\\Users\\Administrator\\Desktop\\777.xlsx");
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
//定义需要合并的列===========================================
ClassInfo first = set.first();
//学院
String academy = first.getAcademy();
AtomicInteger academyIndex = new AtomicInteger(1);
//系
String system = first.getSystem();
AtomicInteger systemIndex = new AtomicInteger(1);
//专业
String specialized = first.getSpecialized();
AtomicInteger specializedIndex = new AtomicInteger(1);
//根据条件合并,出现不同的字段名称,则将该行号与之前的行号之间的行合并。
AtomicInteger count = new AtomicInteger(1);
for (ClassInfo classInfo : set) {
academy = marge(academy, classInfo.getAcademy(), academyIndex, count, writer, 0);
system = marge(system, classInfo.getSystem(), systemIndex, count, writer, 1);
specialized = marge(specialized, classInfo.getSpecialized(), specializedIndex, count, writer, 2);
count.incrementAndGet();
}
//结尾合并
count.decrementAndGet();
endMarge(academyIndex.get(), count.get(), 0, academy, writer);
endMarge(systemIndex.get(), count.get(), 0, system, writer);
endMarge(specializedIndex.get(), count.get(), 0, specialized, writer);
//write
writer.setColumnWidth(-1, 40);
writer.write(set);
writer.flush();
writer.close();
}
/**
* 尾行合并
*/
private static void endMarge(int startIndex, int endIndex, int row, String title, BigExcelWriter writer) {
if ((startIndex == endIndex) || (startIndex > endIndex)) {
return;
}
writer.merge(startIndex, endIndex, row, row, title, true);
}
/**
* 判断是否需要合并
*/
private static String marge(String oldValue, String newValue, AtomicInteger index, AtomicInteger count, BigExcelWriter writer, int row) {
if (StrUtil.equals(oldValue, newValue)) {
return oldValue;
}
int endIndex = count.get() - 1;
if (index.get() == endIndex) {
index.incrementAndGet();
return newValue;
}
writer.merge(index.get(), endIndex, row, row, oldValue, true);
index.set(endIndex);
index.incrementAndGet();
return newValue;
}
/**
* 生成测试数据
*/
@NotNull
private static TreeSet<ClassInfo> getClassInfos() {
TreeSet<ClassInfo> set = new TreeSet<>((o1, o2) -> {
int compareAcademy = o1.getAcademy().compareTo(o2.getAcademy());
if (compareAcademy != 0) return compareAcademy;
int compareSystem = o1.getSystem().compareTo(o2.getSystem());
if (compareSystem != 0) return compareSystem;
int compareSpecialized = o1.getSpecialized().compareTo(o2.getSpecialized());
if (compareSpecialized != 0) return compareSpecialized;
return o1.getClassName().compareTo(o2.getClassName());
});
set.add(new ClassInfo("绘画艺术学院", "油画系", "油画", "1班"));
set.add(new ClassInfo("绘画艺术学院", "油画系", "油画", "2班"));
set.add(new ClassInfo("工业设计学院", "直属学院", "产品设计", "20产品甲"));
set.add(new ClassInfo("绘画艺术学院", "油画系", "油画", "3班"));
set.add(new ClassInfo("工业设计学院", "直属学院", "产品设计", "21产品乙"));
set.add(new ClassInfo("绘画艺术学院", "壁画系", "壁画", "4班"));
set.add(new ClassInfo("绘画艺术学院", "版画系", "版画", "4班"));
set.add(new ClassInfo("绘画艺术学院", "壁画系", "壁画", "4班"));
set.add(new ClassInfo("绘画艺术学院", "-", "美术学(硕士)", "-"));
set.add(new ClassInfo("绘画艺术学院", "版画系", "版画", "4班"));
set.add(new ClassInfo("绘画艺术学院", "-", "美术(硕士)", "-"));
set.add(new ClassInfo("工业设计学院", "直属学院", "产品设计", "20产品丁"));
set.add(new ClassInfo("绘画艺术学院", "油画系", "油画", "4班"));
set.add(new ClassInfo("时尚设计学院", "服装设计系", "服装与服饰设计(服装设计)", "21服装乙"));
set.add(new ClassInfo("时尚设计学院", "服装设计系", "服装与服饰设计(服装设计)", "20服装甲"));
set.add(new ClassInfo("时尚设计学院", "服装设计系", "服装与服饰设计(服装设计)", "22时尚设计4班"));
set.add(new ClassInfo("时尚设计学院", "服装设计系", "服装与服饰设计(染织设计)", "21染织"));
set.add(new ClassInfo("时尚设计学院", "服装设计系", "服装与服饰设计(染织设计)", "20染织"));
return set;
}
}
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.15</version>
</dependency>