前言:对excel文件操作的封装有 easyPoi, Hutool等已经很完善了。但还是有些不尽人意,本工具就是基于 Hutool的进一步封装。想让文件导出更加方便
注意:该工具使用新注解@Header表示表头,和原Hutool中的@Alias,@PropIgnore两个注解会有冲突,避免同时使用,否则会导致导出异常
效果:
?????????
代码:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Simple {
@Header("ID")
private int id;
@Header("名称")
private String name;
@Header("布尔")
private boolean flag;
@Header("数量")
private double count;
}
/**
* 测试导出简单数据
*/
@Test
public void testSimple() {
File file = new File("./file/simple.xlsx");
ExcelWriter excelWriter = ExcelUtil.getWriter(file);
List<Simple> simpleList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
simpleList.add(new Simple(i, "test" + i, true, i * 10));
}
DataKit.exportXlsx(excelWriter, simpleList, null);
excelWriter.close();
}
?效果:?
代码:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Merge {
@Header("ID")
private int id;
@Header("名称")
private String name;
@Header("布尔")
private boolean flag;
@Header({"统计", "数量1"})
private double cnt1;
@Header({"统计", "数量2"})
private double cnt2;
@Header({"统计", "数量3"})
private double cnt3;
@Header({"测试1", "测试"})
private String test1;
@Header({"测试2", "测试"})
private String test2;
@Header({"合并", "数量"})
private double val1;
@Header({"合并", "数量"})
private double val2;
}
/**
* 测试导出合并头部数据
*/
@Test
public void testMerge() {
File file = new File("./file/merge.xlsx");
ExcelWriter excelWriter = ExcelUtil.getWriter(file);
List<Merge> mergeList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
mergeList.add(new Merge(i, "test" + i, true, i * 10, i / 10d, i * 8, "demo" + i, "abc" + i, i / 8d, i * 5.5));
}
DataKit.exportXlsx(excelWriter, mergeList, null);
excelWriter.close();
}
?解析:表头值是数组,每一个下标对应一行,在同行和同列的名称如果相同将进行合并。如果有多行是,而某列数组只有一个值则向对应列合并。具体可以观察案例了解
?效果:
??????????
代码:
/**
* 测试导出简单数据携带图片
*/
@Test
public void testSimpleWithImg() {
File file = new File("./file/simple-img.xlsx");
ExcelWriter excelWriter = ExcelUtil.getWriter(file);
List<Simple> simpleList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
simpleList.add(new Simple(i, "test" + i, true, i * 10));
}
// 加上图片路径回调,可以更加 数据内容来动态对于图片路径
DataKit.exportXlsx(excelWriter, simpleList, (simple)-> "https://www.baidu.com/img/flexible/logo/pc/result.png");
excelWriter.close();
}
?效果:
??????????
代码:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Order {
@Header(value = "ID", order = 3)
private int id;
@Header(value = "名称", order = 0)
private String name;
@Header(value = "布尔", order = 2)
private boolean flag;
@Header(value = "数量", order = 1)
private double count;
}
/**
* 测试导出简单带排序数据
*/
@Test
public void testOrder() {
File file = new File("./file/order.xlsx");
ExcelWriter excelWriter = ExcelUtil.getWriter(file);
List<Order> orderList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
orderList.add(new Order(i, "test" + i, true, i * 10));
}
DataKit.exportXlsx(excelWriter, orderList, null);
excelWriter.close();
}
?效果:
??????????
代码:
/**
* 测试使用csv文件方式,导出大量数据
*/
@Test
public void testExportBigdataToCsvByRange() {
File file = new File("./file/bigdata.csv");
// 定义开始页,
int page = 1;
// 定义每页查询的数量
int limit = 1000;
// 分段加载数据写入文件
DataKit.exportCsvByRange(file, page, limit, (p, l) -> {
if (p == 5) {
return null;
}
// 模拟数据获取,真实环境这里去查询数据库
List<Simple> simpleList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
simpleList.add(new Simple((p - 1) * 10 + i, "test" + i, true, i * 10));
}
return simpleList;
});
}
|