From 8109c77fed6210f8a7a88867241e6eed6e8ca6b2 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 8 Feb 2023 20:24:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E6=96=87=E4=BB=B6=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easyexcel/test/demo/rare/WriteTest.java | 70 +++++++++++++++++++ .../easyexcel/test/demo/write/DemoData.java | 3 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java index c35d7f96..7e5d54a2 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/rare/WriteTest.java @@ -1,8 +1,11 @@ package com.alibaba.easyexcel.test.demo.rare; import java.io.File; +import java.util.ArrayList; import java.util.Date; +import java.util.Iterator; import java.util.List; +import java.util.UUID; import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; @@ -24,6 +27,14 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.ehcache.Cache; +import org.ehcache.Cache.Entry; +import org.ehcache.CacheManager; +import org.ehcache.config.CacheConfiguration; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.CacheManagerBuilder; +import org.ehcache.config.builders.ResourcePoolsBuilder; +import org.ehcache.config.units.MemoryUnit; import org.junit.Ignore; import org.junit.Test; @@ -46,6 +57,7 @@ public class WriteTest { log.info("临时的xml存储在:{}", FileUtils.getPoiFilesPath()); File file = TestFileUtil.createNewFile("rare/compressedTemporaryFile" + System.currentTimeMillis() + ".xlsx"); + log.info("写入到文件:{}", file.getAbsolutePath()); // 这里 需要指定写用哪个class去写 try (ExcelWriter excelWriter = EasyExcel.write(file, DemoData.class).registerWriteHandler( @@ -86,6 +98,7 @@ public class WriteTest { public void specifiedCellWrite() { File file = TestFileUtil.createNewFile("rare/specifiedCellWrite" + System.currentTimeMillis() + ".xlsx"); + log.info("写入到文件:{}", file.getAbsolutePath()); // 需要区分是在 最后一行之前 还是之后 // 区分的原因是:excel只能一直向前,而且内存里面只存储100条,而afterRowDispose是在每一行写入完成的时候调用,所以修改一行需要拦截这个事件 @@ -128,6 +141,63 @@ public class WriteTest { log.info("写入到文件完成:{}", file); } + /** + * 缓存配置 + */ + private static final CacheConfiguration CACHE_CONFIGURATION = CacheConfigurationBuilder + // 设置产品最大占用10G + .newCacheConfigurationBuilder(String.class, ArrayList.class, + ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)) + // 最大遍历深度 设置成一直遍历 + .withSizeOfMaxObjectGraph(1000 * 1000L) + // 最大对象为10G数据 + .withSizeOfMaxObjectSize(10, MemoryUnit.GB).build(); + + /** + *缓存存储文件的位置 + */ + private static final String CACHE_PATH = FileUtils.getTempFilePrefix() + "myCache"; + private static final CacheManager CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder() + // 这里名字随便取 路径放到临时目录 + .with(CacheManagerBuilder.persistence(CACHE_PATH)) + .build(true); + + /** + * 在大文件后追加写入 + * 这个是无法实现的,所以思路就是临时存储到磁盘里面,然后再读取出来一次性写入 + */ + @Test + public void appendLagreFile() { + log.info("临时的缓存目录是:{}", CACHE_PATH); + + + + // 记录缓存名字 + String cacheName = UUID.randomUUID().toString(); + // 整个文件的要一起公用 + Cache> cache = cacheManager.createCache(cacheName, cacheConfiguration); + + for (int i = 0; i < 10000; i++) { + // 把所有需要追加的数据放到缓存里面 + cache.put(UUID.randomUUID().toString(), data()); + } + + File file = TestFileUtil.createNewFile("rare/appendLagreFile" + System.currentTimeMillis() + + ".xlsx"); + log.info("写入到文件:{}", file.getAbsolutePath()); + // 这里 需要指定写用哪个class去写 + try (ExcelWriter excelWriter = EasyExcel.write(file, DemoData.class).build()) { + // 这里注意 如果同一个sheet只要创建一次 + WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); + Iterator>> iterator = cache.iterator(); + while (iterator.hasNext()) { + excelWriter.write(iterator.next().getValue(), writeSheet); + } + log.info("写入数据完毕。"); + } + cacheManager.removeCache(cacheName); + } + private List data() { List list = ListUtils.newArrayList(); for (int i = 0; i < 10; i++) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java index fc3079a1..e1669023 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java @@ -1,5 +1,6 @@ package com.alibaba.easyexcel.test.demo.write; +import java.io.Serializable; import java.util.Date; import com.alibaba.excel.annotation.ExcelIgnore; @@ -17,7 +18,7 @@ import lombok.Setter; @Getter @Setter @EqualsAndHashCode -public class DemoData { +public class DemoData implements Serializable { @ExcelProperty("字符串标题") private String string; @ExcelProperty("日期标题")