Repository: wangguanquan/eec
Pull request #70 (2019-08-13 14:20:17)

Fix#69
#69 Fix miss the style when null value


Patch:
diff --git a/src/main/java/org/ttzero/excel/entity/ListMapSheet.java b/src/main/java/org/ttzero/excel/entity/ListMapSheet.java
index 933c20d4..75ec00da 100644
--- a/src/main/java/org/ttzero/excel/entity/ListMapSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListMapSheet.java
@@ -17,12 +17,13 @@
 package org.ttzero.excel.entity;
 
 import org.ttzero.excel.reader.Cell;
-import org.ttzero.excel.util.StringUtil;
 
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import static org.ttzero.excel.util.StringUtil.isEmpty;
+
 /**
  * ListMapSheet is a subclass of {@link ListSheet}, the difference is
  * in the way the data is taken
@@ -157,12 +158,6 @@ public class ListMapSheet extends ListSheet<Map<String, ?>> {
                 Cell cell = cells[i];
                 cell.clear();
 
-                // blank cell
-                if (e == null) {
-                    cell.setBlank();
-                    continue;
-                }
-
                 setCellValueAndStyle(cell, e, hc);
             }
         }
@@ -188,12 +183,15 @@ public class ListMapSheet extends ListSheet<Map<String, ?>> {
             columns = new Column[size];
             for (Iterator<? extends Map.Entry<String, ?>> it = first.entrySet().iterator(); it.hasNext(); ) {
                 Map.Entry<String, ?> entry = it.next();
-                columns[i++] = new Column(entry.getKey(), entry.getKey(), entry.getValue().getClass());
+                // Ignore the null key
+                if (isEmpty(entry.getKey())) continue;
+                Object value = entry.getValue();
+                columns[i++] = new Column(entry.getKey(), entry.getKey(), value != null ? value.getClass() : String.class);
             }
         } else {
             for (int i = 0; i < columns.length; i++) {
                 Column hc = columns[i];
-                if (StringUtil.isEmpty(hc.key)) {
+                if (isEmpty(hc.key)) {
                     throw new ExcelWriteException(getClass() + " must specify the 'key' name.");
                 }
                 if (hc.getClazz() == null) {
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index 0aebb190..1122783e 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -240,11 +240,6 @@ public class ListSheet<T> extends Sheet {
                     cell.clear();
 
                     Object e = field.get(data.get(start));
-                    // blank cell
-                    if (e == null) {
-                        cell.setBlank();
-                        continue;
-                    }
 
                     setCellValueAndStyle(cell, e, columns[i]);
                 }
diff --git a/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java b/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
index 170b0c2e..6cba5759 100644
--- a/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
@@ -210,12 +210,6 @@ public class ResultSetSheet extends Sheet {
 
                     Object e = rs.getObject(i);
 
-                    // blank cell
-                    if (e == null) {
-                        cell.setBlank();
-                        continue;
-                    }
-
                     setCellValueAndStyle(cell, e, hc);
                 }
             }
diff --git a/src/main/java/org/ttzero/excel/entity/Sheet.java b/src/main/java/org/ttzero/excel/entity/Sheet.java
index 889f85eb..8d9885a1 100644
--- a/src/main/java/org/ttzero/excel/entity/Sheet.java
+++ b/src/main/java/org/ttzero/excel/entity/Sheet.java
@@ -1248,6 +1248,10 @@ public abstract class Sheet implements Cloneable, Storageable {
      * @param clazz the cell value type
      */
     protected void setCellValue(Cell cell, Object e, Column hc, Class<?> clazz) {
+        if (e == null) {
+            setNullValue(cell, hc);
+            return;
+        }
         boolean hasIntProcessor = hc.processor != null;
         if (isString(clazz)) {
             cell.setSv(e.toString());
@@ -1290,6 +1294,20 @@ public abstract class Sheet implements Cloneable, Storageable {
         }
     }
 
+    /**
+     * Setting cell value as null
+     *
+     * @param cell  the cell
+     * @param hc    the header column
+     */
+    protected void setNullValue(Cell cell, Column hc) {
+        boolean hasIntProcessor = hc.processor != null;
+        if (hasIntProcessor) {
+             conversion(cell, 0, hc);
+        } else
+            cell.setBlank();
+    }
+
     /**
      * Returns the cell style index
      *


Test patch:
diff --git a/src/test/java/org/ttzero/excel/entity/ListMapSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListMapSheetTest.java
index ce036e4c..226a2ef4 100644
--- a/src/test/java/org/ttzero/excel/entity/ListMapSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListMapSheetTest.java
@@ -244,6 +244,13 @@ public class ListMapSheetTest extends WorkbookTest {
             .writeTo(defaultTestPath);
     }
 
+    @Test public void testNullValue() throws IOException {
+        new Workbook("test map null value", author)
+            .watch(Print::println)
+            .addSheet(createNullTestData(10))
+            .writeTo(defaultTestPath);
+    }
+
     static List<Map<String, ?>> createTestData() {
         int size = random.nextInt(100) + 1;
         return createTestData(size);
@@ -288,4 +295,15 @@ public class ListMapSheetTest extends WorkbookTest {
         }
         return list;
     }
+
+    static List<Map<String, ?>> createNullTestData(int size) {
+        List<Map<String, ?>> list = new ArrayList<>(size);
+        for (int i = 0; i < size; i++) {
+            Map<String, Object> map = new HashMap<>();
+            map.put("id", random.nextInt());
+            map.put("name", null);
+            list.add(map);
+        }
+        return list;
+    }
 }
diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
index b9a29cf7..59b4c67e 100644
--- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
@@ -325,6 +325,18 @@ public class ListObjectSheetTest extends WorkbookTest{
             .writeTo(defaultTestPath);
     }
 
+    @Test public void testNullValue() throws IOException {
+        new Workbook("test null value", author)
+            .watch(Print::println)
+            .setAutoSize(true)
+            .addSheet(new ListSheet<>("EXT-ITEM", ExtItem.randomTestData(10)
+                , new Sheet.Column("学号", "id", Integer.class, i -> i == 0 ? -1 : i)
+                , new Sheet.Column("姓名", "name", String.class)
+                , new Sheet.Column("妮称", "nice", String.class))
+            )
+            .writeTo(defaultTestPath);
+    }
+
     public static class Item {
         private int id;
         private String name;
@@ -477,4 +489,22 @@ public class ListObjectSheetTest extends WorkbookTest{
         }
     }
 
+    public static class ExtItem {
+        private Integer id;
+        private String name;
+        private String nice;
+
+        ExtItem(String name) {
+            this.name = name;
+        }
+
+        public static List<ExtItem> randomTestData(int n) {
+            List<ExtItem> list = new ArrayList<>(n);
+            for (int i = 0; i < n; i++) {
+                list.add(new ExtItem(getRandomString()));
+            }
+            return list;
+        }
+    }
+
 }