headMap, AnalysisContext context) {
+ LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
+ }
+```
+##### 代码
+```java
+ /**
+ * 读取表头数据
+ *
+ *
+ * 1. 创建excel对应的实体对象 参照{@link DemoData}
+ *
+ * 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoHeadDataListener}
+ *
+ * 3. 直接读即可
+ */
+ @Test
+ public void headerRead() {
+ String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
+ // 这里 需要指定读用哪个class去读,然后读取第一个sheet 然后千万别忘记 finish
+ EasyExcel.read(fileName, DemoData.class, new DemoHeadDataListener()).sheet().doRead();
+ }
+```
+
+### 数据转换等异常处理
+##### excel示例
+参照:[excel示例](#simpleReadExcel)
+##### 对象
+参照:[对象](#simpleReadObject)
+##### 监听器
+参照:[监听器](#simpleReadListener)
+里面多了一个方法,只要重写onException方法即可
+```java
+ /**
+ * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
+ *
+ * @param exception
+ * @param context
+ * @throws Exception
+ */
+ @Override
+ public void onException(Exception exception, AnalysisContext context) {
+ LOGGER.error("解析失败,但是继续解析下一行", exception);
+ }
+```
+##### 代码
+```java
+ /**
+ * 数据转换等异常处理
+ *
+ *
+ * 1. 创建excel对应的实体对象 参照{@link DemoData}
+ *
+ * 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoHeadDataListener}
+ *
+ * 3. 直接读即可
+ */
+ @Test
+ public void exceptionRead() {
+ String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
+ // 这里 需要指定读用哪个class去读,然后读取第一个sheet 然后千万别忘记 finish
+ EasyExcel.read(fileName, DemoData.class, new DemoHeadDataListener()).sheet().doRead();
+ }
+```
+
+
### web中的读
##### 示例代码
DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java](/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java)
diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
index d21b8db..5adc237 100644
--- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
+++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
@@ -15,6 +15,7 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
+import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
@@ -113,40 +114,44 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (readWorkbookHolder.getReadCache() != null) {
readWorkbookHolder.getReadCache().destroy();
}
- } catch (Throwable e) {
- throw new ExcelAnalysisException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (readWorkbookHolder.getOpcPackage() != null) {
readWorkbookHolder.getOpcPackage().close();
}
- } catch (Throwable e) {
- throw new ExcelAnalysisException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (readWorkbookHolder.getPoifsFileSystem() != null) {
readWorkbookHolder.getPoifsFileSystem().close();
}
- } catch (Throwable e) {
- throw new ExcelAnalysisException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (analysisContext.readWorkbookHolder().getAutoCloseStream()
&& readWorkbookHolder.getInputStream() != null) {
readWorkbookHolder.getInputStream().close();
}
- } catch (Throwable e) {
- throw new ExcelAnalysisException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (readWorkbookHolder.getTempFile() != null) {
FileUtils.delete(readWorkbookHolder.getTempFile());
}
- } catch (Throwable e) {
- throw new ExcelAnalysisException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
}
+ private void throwCanNotCloseIo(Throwable t) {
+ throw new ExcelAnalysisException("Can not close IO", t);
+ }
+
@Override
public com.alibaba.excel.analysis.ExcelExecutor excelExecutor() {
return excelExecutor;
diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
index ebe0d26..6ad7fcb 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
@@ -9,6 +9,7 @@ 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.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -343,39 +344,52 @@ public class WriteContextImpl implements WriteContext {
if (writeWorkbookHolder == null) {
return;
}
+
try {
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
writeWorkbookHolder.getWorkbook().close();
- } catch (Throwable e) {
- throw new ExcelGenerateException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
+ }
+ try {
+ Workbook workbook = writeWorkbookHolder.getWorkbook();
+ if (workbook instanceof SXSSFWorkbook) {
+ ((SXSSFWorkbook)workbook).dispose();
+ }
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
}
- } catch (Throwable e) {
- throw new ExcelGenerateException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) {
writeWorkbookHolder.getTemplateInputStream().close();
}
- } catch (Throwable e) {
- throw new ExcelGenerateException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
try {
if (!writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getFile() != null
&& writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
}
- } catch (Throwable e) {
- throw new ExcelGenerateException("Can not close IO", e);
+ } catch (Throwable t) {
+ throwCanNotCloseIo(t);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Finished write.");
}
}
+ private void throwCanNotCloseIo(Throwable t) {
+ throw new ExcelGenerateException("Can not close IO", t);
+ }
+
@Override
public Sheet getCurrentSheet() {
return writeSheetHolder.getSheet();
diff --git a/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java b/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java
index 4d596db..89bca75 100644
--- a/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java
+++ b/src/main/java/com/alibaba/excel/event/AnalysisEventListener.java
@@ -1,7 +1,11 @@
package com.alibaba.excel.event;
+import java.util.Map;
+
import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.read.listener.ReadListener;
+import com.alibaba.excel.util.ConverterUtils;
/**
* Receives the return of each piece of data parsed
@@ -10,6 +14,19 @@ import com.alibaba.excel.read.listener.ReadListener;
*/
public abstract class AnalysisEventListener implements ReadListener {
+ @Override
+ public void invokeHead(Map headMap, AnalysisContext context) {
+ invokeHeadMap(ConverterUtils.convertToStringMap(headMap, context.currentReadHolder()), context);
+ }
+
+ /**
+ * Returns the header as a map.Override the current method to receive header data.
+ *
+ * @param headMap
+ * @param context
+ */
+ public void invokeHeadMap(Map headMap, AnalysisContext context) {}
+
/**
* All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the
* entire read will terminate.
diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
index bf4a8cf..522369e 100644
--- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
+++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
@@ -18,6 +18,7 @@ import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
+import com.alibaba.excel.util.ConverterUtils;
import net.sf.cglib.beans.BeanMap;
@@ -28,6 +29,9 @@ import net.sf.cglib.beans.BeanMap;
*/
public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener