|
|
|
@ -83,14 +83,28 @@ public class FillTest {
|
|
|
|
|
return data(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 方案3 分多次 填充 会使用文件缓存(省内存)
|
|
|
|
|
// 方案3.1 分多次 填充 会使用文件缓存(省内存) 使用 try-with-resources @since 3.1.0
|
|
|
|
|
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; |
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
|
// 千万别忘记关闭流
|
|
|
|
|
excelWriter.finish(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 方案3.2 分多次 填充 会使用文件缓存(省内存) 不使用 try-with-resources
|
|
|
|
|
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx"; |
|
|
|
|
ExcelWriter excelWriter = null; |
|
|
|
|
try { |
|
|
|
|
excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
|
} finally { |
|
|
|
|
// 千万别忘记close 会帮忙关闭流
|
|
|
|
|
if (excelWriter != null) { |
|
|
|
|
excelWriter.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -106,7 +120,8 @@ public class FillTest {
|
|
|
|
|
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complex.xlsx"; |
|
|
|
|
|
|
|
|
|
String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx"; |
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
// 方案1 : 使用 try-with-resources @since 3.1.0
|
|
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
// 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
|
|
|
|
// forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
|
|
|
@ -119,7 +134,30 @@ public class FillTest {
|
|
|
|
|
map.put("date", "2019年10月9日13:28:28"); |
|
|
|
|
map.put("total", 1000); |
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
excelWriter.finish(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 方案2 : 不使用 try-with-resources
|
|
|
|
|
ExcelWriter excelWriter = null; |
|
|
|
|
try { |
|
|
|
|
excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
// 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
|
|
|
|
// forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
|
|
|
|
// 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
|
|
|
|
|
// 如果数据量大 list不是最后一行 参照下一个
|
|
|
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); |
|
|
|
|
excelWriter.fill(data(), fillConfig, writeSheet); |
|
|
|
|
excelWriter.fill(data(), fillConfig, writeSheet); |
|
|
|
|
Map<String, Object> map = MapUtils.newHashMap(); |
|
|
|
|
map.put("date", "2019年10月9日13:28:28"); |
|
|
|
|
map.put("total", 1000); |
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
} finally { |
|
|
|
|
// 千万别忘记close 会帮忙关闭流
|
|
|
|
|
if (excelWriter != null) { |
|
|
|
|
excelWriter.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -138,7 +176,9 @@ public class FillTest {
|
|
|
|
|
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complexFillWithTable.xlsx"; |
|
|
|
|
|
|
|
|
|
String fileName = TestFileUtil.getPath() + "complexFillWithTable" + System.currentTimeMillis() + ".xlsx"; |
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
|
|
|
|
|
// 方案1 : 使用 try-with-resources @since 3.1.0
|
|
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
// 直接写入数据
|
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
@ -161,11 +201,47 @@ public class FillTest {
|
|
|
|
|
totalList.add("统计:1000"); |
|
|
|
|
// 这里是write 别和fill 搞错了
|
|
|
|
|
excelWriter.write(totalListList, writeSheet); |
|
|
|
|
excelWriter.finish(); |
|
|
|
|
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
|
|
|
|
|
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 方案2 : 不使用 try-with-resources
|
|
|
|
|
ExcelWriter excelWriter = null; |
|
|
|
|
try { |
|
|
|
|
excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
// 直接写入数据
|
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
|
excelWriter.fill(data(), writeSheet); |
|
|
|
|
|
|
|
|
|
// 写入list之前的数据
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
map.put("date", "2019年10月9日13:28:28"); |
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
|
|
|
|
|
// list 后面还有个统计 想办法手动写入
|
|
|
|
|
// 这里偷懒直接用list 也可以用对象
|
|
|
|
|
List<List<String>> totalListList = ListUtils.newArrayList(); |
|
|
|
|
List<String> totalList = ListUtils.newArrayList(); |
|
|
|
|
totalListList.add(totalList); |
|
|
|
|
totalList.add(null); |
|
|
|
|
totalList.add(null); |
|
|
|
|
totalList.add(null); |
|
|
|
|
// 第四列
|
|
|
|
|
totalList.add("统计:1000"); |
|
|
|
|
// 这里是write 别和fill 搞错了
|
|
|
|
|
excelWriter.write(totalListList, writeSheet); |
|
|
|
|
|
|
|
|
|
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
|
|
|
|
|
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
|
|
|
|
|
} finally { |
|
|
|
|
// 千万别忘记close 会帮忙关闭流
|
|
|
|
|
if (excelWriter != null) { |
|
|
|
|
excelWriter.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 横向的填充 |
|
|
|
|
* |
|
|
|
@ -179,18 +255,36 @@ public class FillTest {
|
|
|
|
|
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "horizontal.xlsx"; |
|
|
|
|
|
|
|
|
|
String fileName = TestFileUtil.getPath() + "horizontalFill" + System.currentTimeMillis() + ".xlsx"; |
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
// 方案1 : 使用 try-with-resources @since 3.1.0
|
|
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); |
|
|
|
|
excelWriter.fill(data(), fillConfig, writeSheet); |
|
|
|
|
excelWriter.fill(data(), fillConfig, writeSheet); |
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
|
map.put("date", "2019年10月9日13:28:28"); |
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 方案2 : 不使用 try-with-resources
|
|
|
|
|
ExcelWriter excelWriter = null; |
|
|
|
|
try { |
|
|
|
|
excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); |
|
|
|
|
excelWriter.fill(data(), fillConfig, writeSheet); |
|
|
|
|
excelWriter.fill(data(), fillConfig, writeSheet); |
|
|
|
|
|
|
|
|
|
// 别忘记关闭流
|
|
|
|
|
excelWriter.finish(); |
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
map.put("date", "2019年10月9日13:28:28"); |
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
} finally { |
|
|
|
|
// 千万别忘记close 会帮忙关闭流
|
|
|
|
|
if (excelWriter != null) { |
|
|
|
|
excelWriter.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -206,7 +300,9 @@ public class FillTest {
|
|
|
|
|
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "composite.xlsx"; |
|
|
|
|
|
|
|
|
|
String fileName = TestFileUtil.getPath() + "compositeFill" + System.currentTimeMillis() + ".xlsx"; |
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
|
|
|
|
|
// 方案1 : 使用 try-with-resources @since 3.1.0
|
|
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); |
|
|
|
|
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
|
|
|
|
@ -222,9 +318,33 @@ public class FillTest {
|
|
|
|
|
map.put("date", new Date()); |
|
|
|
|
|
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 别忘记关闭流
|
|
|
|
|
excelWriter.finish(); |
|
|
|
|
// 方案2 : 不使用 try-with-resources
|
|
|
|
|
ExcelWriter excelWriter = null; |
|
|
|
|
try { |
|
|
|
|
excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
|
|
|
|
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); |
|
|
|
|
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
|
|
|
|
|
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); |
|
|
|
|
excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); |
|
|
|
|
excelWriter.fill(new FillWrapper("data2", data()), writeSheet); |
|
|
|
|
excelWriter.fill(new FillWrapper("data2", data()), writeSheet); |
|
|
|
|
excelWriter.fill(new FillWrapper("data3", data()), writeSheet); |
|
|
|
|
excelWriter.fill(new FillWrapper("data3", data()), writeSheet); |
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
|
//map.put("date", "2019年10月9日13:28:28");
|
|
|
|
|
map.put("date", new Date()); |
|
|
|
|
|
|
|
|
|
excelWriter.fill(map, writeSheet); |
|
|
|
|
} finally { |
|
|
|
|
// 千万别忘记close 会帮忙关闭流
|
|
|
|
|
if (excelWriter != null) { |
|
|
|
|
excelWriter.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<FillData> data() { |
|
|
|
|