From c6f21608abaf933b880e180a4ed6ee6c1d3f7109 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 3 Jan 2020 20:08:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A1=AB=E5=85=85=E5=8F=AA?= =?UTF-8?q?=E6=9C=89`sheetName`=E4=BC=9A=E6=8A=9B=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + pom.xml | 2 +- .../excel/context/WriteContextImpl.java | 25 +++++++++++++------ update.md | 3 +++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1251ebd2..b142b9dd 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ easyexcel [QQ群: 662022184](//shang.qq.com/wpa/qunwpa?idkey=53d9d821b0833e3c14670f007488a61e300f00ff4f1b81fd950590d90dd80f80) [钉钉群: 21960511](https://qr.dingtalk.com/action/joingroup?code=v1,k1,cchz6k12ci9B08NNqhNRFGXocNVHrZtW0kaOtTKg/Rk=&_dt_no_comment=1&origin=11) [官方网站: https://alibaba-easyexcel.github.io/](https://alibaba-easyexcel.github.io/) +#### 因为公司不方便用QQ,所以建议加钉钉群 # JAVA解析Excel工具easyexcel Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便 diff --git a/pom.xml b/pom.xml index 844f70c4..fe46977f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.1.5 + 2.1.6 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index df4840d0..e79453c7 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -161,14 +161,10 @@ public class WriteContextImpl implements WriteContext { .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheet(writeSheetHolder.getSheetName())); } } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Can not find sheet:{} ,now create it", writeSheetHolder.getSheetNo()); - } - if (StringUtils.isEmpty(writeSheetHolder.getSheetName())) { - writeSheetHolder.setSheetName(writeSheetHolder.getSheetNo().toString()); - } - currentSheet = WorkBookUtil.createSheet(writeWorkbookHolder.getWorkbook(), writeSheetHolder.getSheetName()); - writeSheetHolder.setCachedSheet(currentSheet); + currentSheet = createSheet(); + } + if (currentSheet == null) { + currentSheet = createSheet(); } writeSheetHolder.setSheet(currentSheet); WriteHandlerUtils.afterSheetCreate(this); @@ -180,6 +176,19 @@ public class WriteContextImpl implements WriteContext { writeWorkbookHolder.getHasBeenInitializedSheetNameMap().put(writeSheetHolder.getSheetName(), writeSheetHolder); } + private Sheet createSheet() { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Can not find sheet:{} ,now create it", writeSheetHolder.getSheetNo()); + } + if (StringUtils.isEmpty(writeSheetHolder.getSheetName())) { + writeSheetHolder.setSheetName(writeSheetHolder.getSheetNo().toString()); + } + Sheet currentSheet = + WorkBookUtil.createSheet(writeWorkbookHolder.getWorkbook(), writeSheetHolder.getSheetName()); + writeSheetHolder.setCachedSheet(currentSheet); + return currentSheet; + } + public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) { if (!currentWriteHolder.needHead() || !currentWriteHolder.excelWriteHeadProperty().hasHead()) { return; diff --git a/update.md b/update.md index 35ecf499..46258104 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.1.6 +* 修复填充只有`sheetName`会抛异常 + # 2.1.5 * 修复部分xlsx没有行号读取异常 * 填充时候支持根据`sheetName`定位`sheet`