Repository: wangguanquan/eec
Pull request #411 (2024-12-19 12:26:11)

#405 reversion功能增强


Patch:
diff --git a/src/main/java/org/ttzero/excel/processor/Converter.java b/src/main/java/org/ttzero/excel/processor/Converter.java
index 8fb97895..28d9c332 100644
--- a/src/main/java/org/ttzero/excel/processor/Converter.java
+++ b/src/main/java/org/ttzero/excel/processor/Converter.java
@@ -1,5 +1,8 @@
 package org.ttzero.excel.processor;
 
+import org.ttzero.excel.reader.Cell;
+import org.ttzero.excel.reader.Row;
+
 /**
  * 转换器，包含{@link #conversion}和{@link #reversion}方法，前者用于输出时将Java数据转为Excel数据，
  * 后者正好相反输入时将Excel数据转为Java数据
@@ -10,11 +13,12 @@ public interface Converter<T> extends ConversionProcessor {
     /**
      * 输入转换器，读取Excel时将单元格的值转为指定类型{@code T}
      *
-     * @param v Excel原始值
-     * @param fieldClazz 导入字段类型
+     * @param row 当前行{@link Row}
+     * @param cell 当前单元格{@link Cell}
+     * @param destClazz 转换后的类型
      * @return 转换后的值
      */
-    T reversion(String v, Class<?> fieldClazz);
+    T reversion(Row row, Cell cell, Class<?> destClazz);
 
     /**
      * 无类型转换，默认
@@ -22,8 +26,8 @@ public interface Converter<T> extends ConversionProcessor {
     final class None implements Converter<Object> {
 
         @Override
-        public Object reversion(String v, Class<?> fieldClazz) {
-            return v;
+        public Object reversion(Row row, Cell cell, Class<?> destClazz) {
+            return row.getString(cell);
         }
 
         @Override
diff --git a/src/main/java/org/ttzero/excel/reader/HeaderRow.java b/src/main/java/org/ttzero/excel/reader/HeaderRow.java
index 56584632..e08de105 100644
--- a/src/main/java/org/ttzero/excel/reader/HeaderRow.java
+++ b/src/main/java/org/ttzero/excel/reader/HeaderRow.java
@@ -489,7 +489,7 @@ public class HeaderRow extends Row {
         int c = ec.colIndex;
         Class<?> fieldClazz = ec.clazz;
         if (ec.converter != null) {
-            ec.field.set(t, ec.converter.reversion(row.getString(c), fieldClazz));
+            ec.field.set(t, ec.converter.reversion(row, row.getCell(c), fieldClazz));
         }
         else if (fieldClazz == String.class) {
             ec.field.set(t, row.getString(c));
@@ -581,7 +581,7 @@ public class HeaderRow extends Row {
         int c = ec.colIndex;
         Class<?> fieldClazz = ec.clazz;
         if (ec.converter != null) {
-            ec.method.invoke(t, ec.converter.reversion(row.getString(c), fieldClazz));
+            ec.method.invoke(t, ec.converter.reversion(row, row.getCell(c), fieldClazz));
         }
         else if (fieldClazz == String.class) {
             ec.method.invoke(t, row.getString(c));


Test patch:
diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest2.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest2.java
index 39e3e3ba..39262064 100644
--- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest2.java
+++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest2.java
@@ -987,9 +987,9 @@ public class ListObjectSheetTest2 extends WorkbookTest {
         final String[] statusDesc = { "未开始", "进行中", "完结", "中止" };
 
         @Override
-        public Integer reversion(String v, Class<?> fieldClazz) {
+        public Integer reversion(org.ttzero.excel.reader.Row row, Cell cell, Class<?> destClazz) {
             for (int i = 0; i < statusDesc.length; i++) {
-                if (statusDesc[i].equals(v)) {
+                if (statusDesc[i].equals(row.getString(cell))) {
                     return i;
                 }
             }
@@ -1142,13 +1142,13 @@ public class ListObjectSheetTest2 extends WorkbookTest {
 
 
         @Override
-        public IBaseEnum reversion(String v, Class<?> fieldClazz) {
+        public IBaseEnum reversion(org.ttzero.excel.reader.Row row, Cell cell, Class<?> destClazz) {
 
-            Map<String, IBaseEnum> clazzIBaseEnumMap = map.get(fieldClazz.getSimpleName());
+            Map<String, IBaseEnum> clazzIBaseEnumMap = map.get(destClazz.getSimpleName());
             if (clazzIBaseEnumMap == null) {
                 return null;
             }
-            return clazzIBaseEnumMap.get(v);
+            return clazzIBaseEnumMap.get(row.getString(cell));
         }
 
         @Override