|
|
@ -1,8 +1,11 @@ |
|
|
|
package com.alibaba.easyexcel.test.demo.rare; |
|
|
|
package com.alibaba.easyexcel.test.demo.rare; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.easyexcel.test.demo.write.DemoData; |
|
|
|
import com.alibaba.easyexcel.test.demo.write.DemoData; |
|
|
|
import com.alibaba.easyexcel.test.util.TestFileUtil; |
|
|
|
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.Sheet; |
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
|
|
|
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.Ignore; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
@ -46,6 +57,7 @@ public class WriteTest { |
|
|
|
log.info("临时的xml存储在:{}", FileUtils.getPoiFilesPath()); |
|
|
|
log.info("临时的xml存储在:{}", FileUtils.getPoiFilesPath()); |
|
|
|
File file = TestFileUtil.createNewFile("rare/compressedTemporaryFile" + System.currentTimeMillis() |
|
|
|
File file = TestFileUtil.createNewFile("rare/compressedTemporaryFile" + System.currentTimeMillis() |
|
|
|
+ ".xlsx"); |
|
|
|
+ ".xlsx"); |
|
|
|
|
|
|
|
log.info("写入到文件:{}", file.getAbsolutePath()); |
|
|
|
|
|
|
|
|
|
|
|
// 这里 需要指定写用哪个class去写
|
|
|
|
// 这里 需要指定写用哪个class去写
|
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(file, DemoData.class).registerWriteHandler( |
|
|
|
try (ExcelWriter excelWriter = EasyExcel.write(file, DemoData.class).registerWriteHandler( |
|
|
@ -86,6 +98,7 @@ public class WriteTest { |
|
|
|
public void specifiedCellWrite() { |
|
|
|
public void specifiedCellWrite() { |
|
|
|
File file = TestFileUtil.createNewFile("rare/specifiedCellWrite" + System.currentTimeMillis() |
|
|
|
File file = TestFileUtil.createNewFile("rare/specifiedCellWrite" + System.currentTimeMillis() |
|
|
|
+ ".xlsx"); |
|
|
|
+ ".xlsx"); |
|
|
|
|
|
|
|
log.info("写入到文件:{}", file.getAbsolutePath()); |
|
|
|
|
|
|
|
|
|
|
|
// 需要区分是在 最后一行之前 还是之后
|
|
|
|
// 需要区分是在 最后一行之前 还是之后
|
|
|
|
// 区分的原因是:excel只能一直向前,而且内存里面只存储100条,而afterRowDispose是在每一行写入完成的时候调用,所以修改一行需要拦截这个事件
|
|
|
|
// 区分的原因是:excel只能一直向前,而且内存里面只存储100条,而afterRowDispose是在每一行写入完成的时候调用,所以修改一行需要拦截这个事件
|
|
|
@ -128,6 +141,63 @@ public class WriteTest { |
|
|
|
log.info("写入到文件完成:{}", file); |
|
|
|
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<String, List<DemoData>> 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<Entry<String, List<DemoData>>> iterator = cache.iterator(); |
|
|
|
|
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
|
|
|
excelWriter.write(iterator.next().getValue(), writeSheet); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
log.info("写入数据完毕。"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
cacheManager.removeCache(cacheName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private List<DemoData> data() { |
|
|
|
private List<DemoData> data() { |
|
|
|
List<DemoData> list = ListUtils.newArrayList(); |
|
|
|
List<DemoData> list = ListUtils.newArrayList(); |
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|