Repository: wangguanquan/eec
Pull request #220 (2021-12-03 01:48:07)

Fix#217


Patch:
diff --git a/src/main/java/org/ttzero/excel/entity/Column.java b/src/main/java/org/ttzero/excel/entity/Column.java
index 61b5fe0c..888737ca 100644
--- a/src/main/java/org/ttzero/excel/entity/Column.java
+++ b/src/main/java/org/ttzero/excel/entity/Column.java
@@ -24,7 +24,7 @@ import org.ttzero.excel.entity.style.Horizontals;
 import org.ttzero.excel.entity.style.NumFmt;
 import org.ttzero.excel.entity.style.Styles;
 import org.ttzero.excel.entity.style.Verticals;
-import org.ttzero.excel.processor.IntConversionProcessor;
+import org.ttzero.excel.processor.ConversionProcessor;
 import org.ttzero.excel.processor.StyleProcessor;
 
 import static org.ttzero.excel.entity.IWorksheetWriter.isBigDecimal;
@@ -69,7 +69,7 @@ public class Column {
     /**
      * The int value conversion
      */
-    public IntConversionProcessor processor;
+    public ConversionProcessor processor;
     /**
      * The style conversion
      */
@@ -158,7 +158,7 @@ public class Column {
      * @param clazz     the cell type
      * @param processor The int value conversion
      */
-    public Column(String name, Class<?> clazz, IntConversionProcessor processor) {
+    public Column(String name, Class<?> clazz, ConversionProcessor processor) {
         this(name, clazz, processor, true);
     }
 
@@ -169,7 +169,7 @@ public class Column {
      * @param key       field
      * @param processor The int value conversion
      */
-    public Column(String name, String key, IntConversionProcessor processor) {
+    public Column(String name, String key, ConversionProcessor processor) {
         this(name, key, processor, true);
     }
 
@@ -207,7 +207,7 @@ public class Column {
      * @param processor The int value conversion
      * @param share     true:shared false:inline string
      */
-    public Column(String name, Class<?> clazz, IntConversionProcessor processor, boolean share) {
+    public Column(String name, Class<?> clazz, ConversionProcessor processor, boolean share) {
         this(name, clazz, share);
         this.processor = processor;
     }
@@ -220,7 +220,7 @@ public class Column {
      * @param clazz     type of cell
      * @param processor The int value conversion
      */
-    public Column(String name, String key, Class<?> clazz, IntConversionProcessor processor) {
+    public Column(String name, String key, Class<?> clazz, ConversionProcessor processor) {
         this(name, key, clazz);
         this.processor = processor;
     }
@@ -233,7 +233,7 @@ public class Column {
      * @param processor The int value conversion
      * @param share     true:shared false:inline string
      */
-    public Column(String name, String key, IntConversionProcessor processor, boolean share) {
+    public Column(String name, String key, ConversionProcessor processor, boolean share) {
         this(name, key, share);
         this.processor = processor;
     }
@@ -355,7 +355,7 @@ public class Column {
      * @param processor The int value conversion
      * @return the {@link Column}
      */
-    public Column setProcessor(IntConversionProcessor processor) {
+    public Column setProcessor(ConversionProcessor processor) {
         this.processor = processor;
         return this;
     }
diff --git a/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java b/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
index e8fbf932..a611fe34 100644
--- a/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
+++ b/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
@@ -77,13 +77,18 @@ public interface ICellValueAndStyle {
      * @param e     the cell value
      * @param hc    the header column
      * @param clazz the cell value type
+     * @param hasProcessor Specify the cell has value converter
      */
-    default void setCellValue(int row, Cell cell, Object e, Column hc, Class<?> clazz) {
+    default void setCellValue(int row, Cell cell, Object e, Column hc, Class<?> clazz, boolean hasProcessor) {
+        if (hasProcessor) {
+            conversion(row, cell, e, hc);
+            return;
+        }
+
         if (e == null) {
             setNullValue(row, cell, hc);
             return;
         }
-        boolean hasIntProcessor = hc.processor != null;
         if (isString(clazz)) {
             cell.setSv(e.toString());
         } else if (isDate(clazz)) {
@@ -91,17 +96,11 @@ public interface ICellValueAndStyle {
         } else if (isDateTime(clazz)) {
             cell.setIv(DateUtil.toDateTimeValue((Timestamp) e));
         } else if (isChar(clazz)) {
-            char c = (Character) e;
-            if (hasIntProcessor) conversion(row, cell, c, hc);
-            else cell.setCv(c);
+            cell.setCv((Character) e);
         } else if (isShort(clazz)) {
-            short t = (Short) e;
-            if (hasIntProcessor) conversion(row, cell, t, hc);
-            else cell.setNv(t);
+            cell.setNv((Short) e);
         } else if (isInt(clazz)) {
-            int n = (Integer) e;
-            if (hasIntProcessor) conversion(row, cell, n, hc);
-            else cell.setNv(n);
+            cell.setNv((Integer) e);
         } else if (isLong(clazz)) {
             cell.setLv((Long) e);
         } else if (isFloat(clazz)) {
@@ -145,11 +144,11 @@ public interface ICellValueAndStyle {
      *
      * @param row the row number
      * @param cell the cell
-     * @param n    the cell value
+     * @param o    the cell value
      * @param hc   the header column
      */
-    default void conversion(int row, Cell cell, int n, Column hc) {
-        Object e = hc.processor.conversion(n);
+    default void conversion(int row, Cell cell, Object o, Column hc) {
+        Object e = hc.processor.conversion(o);
         if (e != null) {
             Class<?> clazz = e.getClass();
             if (isInt(clazz)) {
@@ -161,7 +160,7 @@ public interface ICellValueAndStyle {
                     cell.setNv((Integer) e);
                 }
             } else {
-                setCellValue(row, cell, e, hc, clazz);
+                setCellValue(row, cell, e, hc, clazz, false);
             }
         } else {
             cell.blank();
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index f8c9e406..fa9baf4a 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -19,7 +19,7 @@ package org.ttzero.excel.entity;
 import org.ttzero.excel.annotation.ExcelColumn;
 import org.ttzero.excel.annotation.HeaderComment;
 import org.ttzero.excel.annotation.HeaderStyle;
-import org.ttzero.excel.processor.IntConversionProcessor;
+import org.ttzero.excel.processor.ConversionProcessor;
 import org.ttzero.excel.reader.Cell;
 import org.ttzero.excel.annotation.IgnoreExport;
 
@@ -766,8 +766,8 @@ public class ListSheet<T> extends Sheet {
     }
 
     public static class EntryColumn extends org.ttzero.excel.entity.Column {
-        Method method;
-        Field field;
+        public Method method;
+        public Field field;
 
         public EntryColumn() { }
         public EntryColumn(String name) {
@@ -785,11 +785,11 @@ public class ListSheet<T> extends Sheet {
             super(name, key, clazz);
         }
 
-        public EntryColumn(String name, Class<?> clazz, IntConversionProcessor processor) {
+        public EntryColumn(String name, Class<?> clazz, ConversionProcessor processor) {
             super(name, clazz, processor);
         }
 
-        public EntryColumn(String name, String key, IntConversionProcessor processor) {
+        public EntryColumn(String name, String key, ConversionProcessor processor) {
             super(name, key, processor);
         }
 
@@ -801,15 +801,15 @@ public class ListSheet<T> extends Sheet {
             super(name, key, share);
         }
 
-        public EntryColumn(String name, Class<?> clazz, IntConversionProcessor processor, boolean share) {
+        public EntryColumn(String name, Class<?> clazz, ConversionProcessor processor, boolean share) {
             super(name, clazz, processor, share);
         }
 
-        public EntryColumn(String name, String key, Class<?> clazz, IntConversionProcessor processor) {
+        public EntryColumn(String name, String key, Class<?> clazz, ConversionProcessor processor) {
             super(name, key, clazz, processor);
         }
 
-        public EntryColumn(String name, String key, IntConversionProcessor processor, boolean share) {
+        public EntryColumn(String name, String key, ConversionProcessor processor, boolean share) {
             super(name, key, processor, share);
         }
 
diff --git a/src/main/java/org/ttzero/excel/entity/Sheet.java b/src/main/java/org/ttzero/excel/entity/Sheet.java
index a07a9550..5a4d4cb2 100644
--- a/src/main/java/org/ttzero/excel/entity/Sheet.java
+++ b/src/main/java/org/ttzero/excel/entity/Sheet.java
@@ -30,7 +30,7 @@ import org.ttzero.excel.entity.style.Styles;
 import org.ttzero.excel.entity.style.Verticals;
 import org.ttzero.excel.manager.Const;
 import org.ttzero.excel.manager.RelManager;
-import org.ttzero.excel.processor.IntConversionProcessor;
+import org.ttzero.excel.processor.ConversionProcessor;
 import org.ttzero.excel.reader.Cell;
 import org.ttzero.excel.util.FileUtil;
 
@@ -235,11 +235,11 @@ public abstract class Sheet implements Cloneable, Storable {
             super(name, key, clazz);
         }
 
-        public Column(String name, Class<?> clazz, IntConversionProcessor processor) {
+        public Column(String name, Class<?> clazz, ConversionProcessor processor) {
             super(name, clazz, processor);
         }
 
-        public Column(String name, String key, IntConversionProcessor processor) {
+        public Column(String name, String key, ConversionProcessor processor) {
             super(name, key, processor);
         }
 
@@ -251,15 +251,15 @@ public abstract class Sheet implements Cloneable, Storable {
             super(name, key, share);
         }
 
-        public Column(String name, Class<?> clazz, IntConversionProcessor processor, boolean share) {
+        public Column(String name, Class<?> clazz, ConversionProcessor processor, boolean share) {
             super(name, clazz, processor, share);
         }
 
-        public Column(String name, String key, Class<?> clazz, IntConversionProcessor processor) {
+        public Column(String name, String key, Class<?> clazz, ConversionProcessor processor) {
             super(name, key, clazz, processor);
         }
 
-        public Column(String name, String key, IntConversionProcessor processor, boolean share) {
+        public Column(String name, String key, ConversionProcessor processor, boolean share) {
             super(name, key, processor, share);
         }
 
diff --git a/src/main/java/org/ttzero/excel/entity/csv/CSVCellValueAndStyle.java b/src/main/java/org/ttzero/excel/entity/csv/CSVCellValueAndStyle.java
index e0f64d81..9499d6a1 100644
--- a/src/main/java/org/ttzero/excel/entity/csv/CSVCellValueAndStyle.java
+++ b/src/main/java/org/ttzero/excel/entity/csv/CSVCellValueAndStyle.java
@@ -55,7 +55,7 @@ public class CSVCellValueAndStyle implements ICellValueAndStyle {
      */
     @Override
     public void reset(int row, Cell cell, Object e, Column hc) {
-        setCellValue(row, cell, e, hc, hc.getClazz());
+        setCellValue(row, cell, e, hc, hc.getClazz(), hc.processor != null);
     }
 
     /**
@@ -77,7 +77,11 @@ public class CSVCellValueAndStyle implements ICellValueAndStyle {
      * @param clazz the cell value type
      */
     @Override
-    public void setCellValue(int row, Cell cell, Object e, Column hc, Class<?> clazz) {
+    public void setCellValue(int row, Cell cell, Object e, Column hc, Class<?> clazz, boolean hasProcessor) {
+        if (hasProcessor) {
+            conversion(row, cell, e, hc);
+            return;
+        }
         if (e == null) {
             setNullValue(row, cell, hc);
             return;
diff --git a/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java b/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java
index faeb3fe0..ee8c6b8e 100644
--- a/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java
+++ b/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java
@@ -48,12 +48,12 @@ public class XMLCellValueAndStyle implements ICellValueAndStyle {
      * Int value conversion to others
      *
      * @param cell the cell
-     * @param n    the cell value
+     * @param o    the cell value
      * @param hc   the header column
      */
     @Override
-    public void conversion(int row, Cell cell, int n, Column hc) {
-        Object e = hc.processor.conversion(n);
+    public void conversion(int row, Cell cell, Object o, Column hc) {
+        Object e = hc.processor.conversion(o);
         if (e != null) {
             Class<?> clazz = e.getClass();
             if (isInt(clazz)) {
@@ -66,9 +66,9 @@ public class XMLCellValueAndStyle implements ICellValueAndStyle {
                 }
                 cell.xf = getStyleIndex(row, hc, e);
             } else {
-                setCellValue(row, cell, e, hc, clazz);
+                setCellValue(row, cell, e, hc, clazz, false);
                 int style = hc.getCellStyle(clazz);
-                cell.xf = getStyleIndex(row, hc, n, style);
+                cell.xf = getStyleIndex(row, hc, o, style);
             }
         } else {
             cell.blank();
@@ -85,7 +85,7 @@ public class XMLCellValueAndStyle implements ICellValueAndStyle {
      */
     @Override
     public void reset(int row, Cell cell, Object e, Column hc) {
-        setCellValue(row, cell, e, hc, hc.getClazz());
+        setCellValue(row, cell, e, hc, hc.getClazz(), hc.processor != null);
         if (hc.processor == null) {
             cell.xf = getStyleIndex(row, hc, e);
         }
diff --git a/src/main/java/org/ttzero/excel/processor/ConversionProcessor.java b/src/main/java/org/ttzero/excel/processor/ConversionProcessor.java
new file mode 100644
index 00000000..cb2d8cc9
--- /dev/null
+++ b/src/main/java/org/ttzero/excel/processor/ConversionProcessor.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017, guanquan.wang@yandex.com All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.ttzero.excel.processor;
+
+/**
+ * A value Conversion
+ * Typically used to convert state values or enumerated values
+ * to meaningful values
+ *
+ * @author guanquan.wang on 2017/10/13.
+ */
+@FunctionalInterface
+public interface ConversionProcessor {
+    /**
+     * A value Converter, the converted value is used as the export value
+     * and the style is also changed accordingly
+     *
+     * @param v the original value
+     * @return the converted value
+     */
+    Object conversion(Object v);
+}
diff --git a/src/main/java/org/ttzero/excel/processor/IntConversionProcessor.java b/src/main/java/org/ttzero/excel/processor/IntConversionProcessor.java
index 481bd01f..7237ad8c 100644
--- a/src/main/java/org/ttzero/excel/processor/IntConversionProcessor.java
+++ b/src/main/java/org/ttzero/excel/processor/IntConversionProcessor.java
@@ -22,9 +22,10 @@ package org.ttzero.excel.processor;
  * to meaningful values
  *
  * @author guanquan.wang on 2017/10/13.
+ * @deprecated replace with {@link ConversionProcessor}
  */
-@FunctionalInterface
-public interface IntConversionProcessor {
+@Deprecated
+public interface IntConversionProcessor extends ConversionProcessor {
     /**
      * The integer value include byte, char, short, int
      *


Test patch:
diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
index 09f0a613..7a6857bb 100644
--- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
@@ -26,7 +26,7 @@ import org.ttzero.excel.entity.style.PatternType;
 import org.ttzero.excel.entity.style.Styles;
 import org.ttzero.excel.manager.Const;
 import org.ttzero.excel.manager.docProps.Core;
-import org.ttzero.excel.processor.IntConversionProcessor;
+import org.ttzero.excel.processor.ConversionProcessor;
 import org.ttzero.excel.processor.StyleProcessor;
 import org.ttzero.excel.reader.Dimension;
 import org.ttzero.excel.reader.ExcelReader;
@@ -127,7 +127,7 @@ public class ListObjectSheetTest extends WorkbookTest {
             .addSheet(Student.randomTestData()
                 , new Column("学号", "id")
                 , new Column("姓名", "name")
-                , new Column("成绩", "score", n -> n < 60 ? "不及格" : n)
+                , new Column("成绩", "score", n -> (int) n < 60 ? "不及格" : n)
             )
             .writeTo(defaultTestPath);
     }
@@ -140,7 +140,7 @@ public class ListObjectSheetTest extends WorkbookTest {
                 , new Column("姓名", "name")
                 , new Column("成绩", "score")
                     .setStyleProcessor((o, style, sst) -> {
-                        if ((int)o < 60) {
+                        if ((int) o < 60) {
                             style = Styles.clearFill(style)
                                 | sst.addFill(new Fill(PatternType.solid, Color.orange));
                         }
@@ -156,7 +156,7 @@ public class ListObjectSheetTest extends WorkbookTest {
             .addSheet(Student.randomTestData()
                 , new Column("学号", "id")
                 , new Column("姓名", "name")
-                , new Column("成绩", "score", n -> n < 60 ? "不及格" : n)
+                , new Column("成绩", "score", n -> (int) n < 60 ? "不及格" : n)
                     .setStyleProcessor((o, style, sst) -> {
                         if ((int)o < 60) {
                             style = Styles.clearFill(style)
@@ -322,7 +322,7 @@ public class ListObjectSheetTest extends WorkbookTest {
     };
 
     // 定义一个int值转换lambda表达式，成绩低于60分显示"不及格"，其余显示正常分数
-    public static IntConversionProcessor conversion = n -> n < 60 ? "不及格" : n;
+    public static ConversionProcessor conversion = n -> (int) n < 60 ? "不及格" : n;
 
     @Test
     public void testStyleConversion1() throws IOException {
diff --git a/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java b/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java
index 150a486d..4c559a4e 100644
--- a/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java
@@ -66,7 +66,7 @@ public class MultiWorksheetTest extends SQLWorkbookTest {
                 .addSheet("Statement", "select id, name, age from student"
                     , new Column("学号", int.class)
                     , new Column("姓名", String.class)
-                    , new Column("年龄", int.class, n -> n > 14 ? "高龄" : n)
+                    , new Column("年龄", int.class, n -> (int) n > 14 ? "高龄" : n)
                         .setStyleProcessor((o, style, sst) -> {
                             int n = (int) o;
                             if (n > 14) {
diff --git a/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java b/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java
index 0e107cbf..8a6f07d6 100644
--- a/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java
@@ -107,7 +107,7 @@ public class StatementSheetTest extends SQLWorkbookTest {
                 .addSheet("select id, name, age, create_date, update_date from student"
                     , new Column("学号", int.class)
                     , new Column("姓名", String.class)
-                    , new Column("年龄", int.class, n -> n > 14 ? "高龄" : n)
+                    , new Column("年龄", int.class, n -> (int) n > 14 ? "高龄" : n)
                         .setStyleProcessor((o, style, sst) -> {
                             Integer n = (Integer) o;
                             if (n == null || n > 14) {
diff --git a/src/test/java/org/ttzero/excel/entity/csv/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/csv/ListObjectSheetTest.java
index d9b022d0..6d757f8e 100644
--- a/src/test/java/org/ttzero/excel/entity/csv/ListObjectSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/csv/ListObjectSheetTest.java
@@ -23,7 +23,6 @@ import org.ttzero.excel.entity.Column;
 import org.ttzero.excel.entity.CustomizeDataSourceSheet;
 import org.ttzero.excel.entity.ExcelWriteException;
 import org.ttzero.excel.entity.ListSheet;
-import org.ttzero.excel.entity.Sheet;
 import org.ttzero.excel.entity.TooManyColumnsException;
 import org.ttzero.excel.entity.Workbook;
 import org.ttzero.excel.entity.WorkbookTest;
@@ -101,7 +100,7 @@ public class ListObjectSheetTest extends WorkbookTest{
             .addSheet(Student.randomTestData()
                 , new Column("学号", "id")
                 , new Column("姓名", "name")
-                , new Column("成绩", "score", n -> n < 60 ? "不及格" : n)
+                , new Column("成绩", "score", n -> (int) n < 60 ? "不及格" : n)
             )
             .saveAsCSV()
             .writeTo(getOutputTestPath());
@@ -132,9 +131,9 @@ public class ListObjectSheetTest extends WorkbookTest{
             .addSheet(Student.randomTestData()
                 , new Column("学号", "id")
                 , new Column("姓名", "name")
-                , new Column("成绩", "score", n -> n < 60 ? "不及格" : n)
+                , new Column("成绩", "score", n -> (int) n < 60 ? "不及格" : n)
                     .setStyleProcessor((o, style, sst) -> {
-                        if ((int)o < 60) {
+                        if ((int) o < 60) {
                             style = Styles.clearFill(style)
                                 | sst.addFill(new Fill(PatternType.solid, new Color(246, 209, 139)));
                         }
diff --git a/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java b/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java
index 3e52d33a..43a8eeca 100644
--- a/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java
@@ -21,7 +21,6 @@ import org.ttzero.excel.Print;
 import org.ttzero.excel.entity.Column;
 import org.ttzero.excel.entity.ExcelWriteException;
 import org.ttzero.excel.entity.SQLWorkbookTest;
-import org.ttzero.excel.entity.Sheet;
 import org.ttzero.excel.entity.StatementSheet;
 import org.ttzero.excel.entity.Workbook;
 import org.ttzero.excel.entity.style.Fill;
@@ -83,7 +82,7 @@ public class StatementSheetTest extends SQLWorkbookTest {
                 .addSheet("select id, name, age from student"
                     , new Column("学号", int.class)
                     , new Column("姓名", String.class)
-                    , new Column("年龄", int.class, n -> n > 14 ? "高龄" : n)
+                    , new Column("年龄", int.class, n -> (int) n > 14 ? "高龄" : n)
                         .setStyleProcessor((o, style, sst) -> {
                             int n = (int) o;
                             if (n > 14) {