- * 1. 创建excel对应的实体对象 参照{@link DemoData} + * 1. 创建excel对应的实体对象 参照{@link ExceptionDemoData} *
- * 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoHeadDataListener} + * 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoExceptionListener} *
* 3. 直接读即可
*/
@Test
public void exceptionRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
- // 这里 需要指定读用哪个class去读,然后读取第一个sheet
- EasyExcel.read(fileName, DemoData.class, new DemoHeadDataListener()).sheet().doRead();
+ // 这里 需要指定读用哪个class去读,然后读取第一个sheet
+ EasyExcel.read(fileName, ExceptionDemoData.class, new DemoExceptionListener()).sheet().doRead();
}
```
@@ -1123,7 +1140,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
*/
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
- // 这里注意 有同学反应下载的文件名不对。这个时候 请别使用swagger 他会有影响
+ // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
index 7134e6f8..129b74e3 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
@@ -92,7 +92,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
@Override
public void execute() {
- analysisContext.readSheetHolder().getSheetNo();
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener);
workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
diff --git a/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java b/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java
index 74630f04..4af25690 100644
--- a/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java
+++ b/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java
@@ -1,16 +1,49 @@
package com.alibaba.excel.exception;
+import com.alibaba.excel.metadata.property.ExcelContentProperty;
+import com.alibaba.excel.write.builder.ExcelWriterBuilder;
+
/**
* Data convert exception
*
* @author Jiaju Zhuang
*/
public class ExcelDataConvertException extends RuntimeException {
+ /**
+ * NotNull.
+ */
+ private Integer rowIndex;
+ /**
+ * NotNull.
+ */
+ private Integer columnIndex;
+ /**
+ * Nullable.Only when the header is configured and when the class header is used is not null.
+ *
+ * @see {@link ExcelWriterBuilder#head(Class)}
+ */
+ private ExcelContentProperty excelContentProperty;
public ExcelDataConvertException(String message) {
super(message);
}
+ public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, ExcelContentProperty excelContentProperty,
+ String message) {
+ super(message);
+ this.rowIndex = rowIndex;
+ this.columnIndex = columnIndex;
+ this.excelContentProperty = excelContentProperty;
+ }
+
+ public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, ExcelContentProperty excelContentProperty,
+ String message, Throwable cause) {
+ super(message, cause);
+ this.rowIndex = rowIndex;
+ this.columnIndex = columnIndex;
+ this.excelContentProperty = excelContentProperty;
+ }
+
public ExcelDataConvertException(String message, Throwable cause) {
super(message, cause);
}
@@ -18,4 +51,28 @@ public class ExcelDataConvertException extends RuntimeException {
public ExcelDataConvertException(Throwable cause) {
super(cause);
}
+
+ public Integer getRowIndex() {
+ return rowIndex;
+ }
+
+ public void setRowIndex(Integer rowIndex) {
+ this.rowIndex = rowIndex;
+ }
+
+ public Integer getColumnIndex() {
+ return columnIndex;
+ }
+
+ public void setColumnIndex(Integer columnIndex) {
+ this.columnIndex = columnIndex;
+ }
+
+ public ExcelContentProperty getExcelContentProperty() {
+ return excelContentProperty;
+ }
+
+ public void setExcelContentProperty(ExcelContentProperty excelContentProperty) {
+ this.excelContentProperty = excelContentProperty;
+ }
}
diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
index 09d55603..4bffce6d 100644
--- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
+++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
@@ -1,6 +1,7 @@
package com.alibaba.excel.metadata.property;
import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -133,6 +134,13 @@ public class ExcelHeadProperty {
ignoreMap.put(field.getName(), field);
continue;
}
+ boolean isStaticFinalOrTransient =
+ (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
+ || Modifier.isTransient(field.getModifiers());
+ if (excelProperty == null && isStaticFinalOrTransient) {
+ ignoreMap.put(field.getName(), field);
+ continue;
+ }
if (excelProperty == null || excelProperty.index() < 0) {
defaultFieldList.add(field);
continue;
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 6bea5931..a368024f 100644
--- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
+++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
@@ -33,7 +33,8 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
public void invoke(Map
* 1. 创建excel对应的实体对象 参照{@link DemoData}
@@ -174,10 +175,27 @@ public class ReadTest {
* 3. 直接读即可
*/
@Test
+ public void cellDataRead() {
+ String fileName = TestFileUtil.getPath() + "demo" + File.separator + "cellDataDemo.xlsx";
+ // 这里 需要指定读用哪个class去读,然后读取第一个sheet
+ EasyExcel.read(fileName, CellDataReadDemoData.class, new CellDataDemoHeadDataListener()).sheet().doRead();
+ }
+
+ /**
+ * 数据转换等异常处理
+ *
+ *
+ * 1. 创建excel对应的实体对象 参照{@link ExceptionDemoData}
+ *
+ * 2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoExceptionListener}
+ *
+ * 3. 直接读即可
+ */
+ @Test
public void exceptionRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet
- EasyExcel.read(fileName, DemoData.class, new DemoHeadDataListener()).sheet().doRead();
+ EasyExcel.read(fileName, ExceptionDemoData.class, new DemoExceptionListener()).sheet().doRead();
}
/**
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
index 8fff02ca..cc525629 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
@@ -36,7 +36,7 @@ public class WebTest {
*/
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
- // 这里注意 有同学反应下载的文件名不对。这个时候 请别使用swagger 他会有影响
+ // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java
index 2e8931cc..fb39de15 100644
--- a/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java
@@ -3,6 +3,7 @@ package com.alibaba.easyexcel.test.temp;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
+import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
@@ -35,4 +36,14 @@ public class LockTest {
}
}
+ @Test
+ public void test2() throws Exception {
+ List