Repository: wangguanquan/eec
Pull request #279 (2022-08-03 01:06:08)

#278 Support hide Column


Patch:
diff --git a/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java b/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java
index 1fffd20c..25717712 100644
--- a/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java
+++ b/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java
@@ -107,4 +107,14 @@ public @interface ExcelColumn {
      * @return max cell width, less than or equal to 0 means unset
      */
     double maxWidth() default 0.0D;
+
+    /**
+     * Hidden current column
+     * <p>
+     * Only set the column to hide, the data will still be written,
+     * you can right-click to "un-hide" to display in file
+     *
+     * @return true: hidden otherwise show
+     */
+    boolean hide() default false;
 }
diff --git a/src/main/java/org/ttzero/excel/entity/Column.java b/src/main/java/org/ttzero/excel/entity/Column.java
index 5cc290e0..44fcde10 100644
--- a/src/main/java/org/ttzero/excel/entity/Column.java
+++ b/src/main/java/org/ttzero/excel/entity/Column.java
@@ -144,7 +144,14 @@ public class Column {
     /**
      * The real col-Index used to write
      */
-    int realColIndex;
+    public int realColIndex;
+    /**
+     * Hidden current column
+     * <p>
+     * Only set the column to hide, the data will still be written,
+     * you can right-click to "un-hide" to display in file
+     */
+    public boolean hide;
 
     /**
      * Constructor Column
@@ -836,4 +843,33 @@ public class Column {
     public int getRealColIndex() {
         return realColIndex;
     }
+
+    /**
+     * Returns hide flag
+     *
+     * @return true: hidden otherwise show
+     */
+    public boolean isHide() {
+        return hide;
+    }
+
+    /**
+     * Hidden current column
+     *
+     * @return current {@link Column}
+     */
+    public Column hide() {
+        this.hide = true;
+        return this;
+    }
+
+    /**
+     * Show current column
+     *
+     * @return current {@link Column}
+     */
+    public Column show() {
+        this.hide = false;
+        return this;
+    }
 }
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index 2c295955..808cf607 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -642,6 +642,8 @@ public class ListSheet<T> extends Sheet {
         if ((isNotEmpty(comment.value()) || isNotEmpty(comment.title()))) {
             column.headerComment = new Comment(comment.title(), comment.value());
         }
+        // Hidden Column
+        if (ec.hide()) column.hide();
         return column;
     }
 
@@ -1028,6 +1030,8 @@ public class ListSheet<T> extends Sheet {
             this.ignoreValue = other.ignoreValue;
             this.wrapText = other.wrapText;
             this.colIndex = other.colIndex;
+            this.hide = other.hide;
+            this.realColIndex = other.realColIndex;
             if (other.cellStyle != null) setCellStyle(other.cellStyle);
             if (other.headerStyle != null) setHeaderStyle(other.headerStyle);
             if (other.next != null) {
diff --git a/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java b/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
index 35abad51..0d93859c 100644
--- a/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
+++ b/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
@@ -1046,7 +1046,11 @@ public class XMLWorksheetWriter implements IWorksheetWriter {
             for (int i = 0; i < columns.length; i++) {
                 Column col = columns[i];
                 bw.write("<col customWidth=\"1\" width=\"");
-                bw.write(col.width > 0.0000001D ? new BigDecimal(col.width).setScale(2, BigDecimal.ROUND_HALF_UP).toString() : defaultWidth);
+                if (col.isHide()) {
+                    bw.writeInt(0);
+                    bw.write("\" hidden=\"1");
+                }
+                else bw.write(col.width > 0.0000001D ? new BigDecimal(col.width).setScale(2, BigDecimal.ROUND_HALF_UP).toString() : defaultWidth);
                 bw.write('"');
                 for (int j = fillSpace - defaultWidth.length(); j-- > 0; ) bw.write(32); // Fill space
                 bw.write(" min=\"");


Test patch:
diff --git a/src/test/java/org/ttzero/excel/entity/HideColumnTest.java b/src/test/java/org/ttzero/excel/entity/HideColumnTest.java
new file mode 100644
index 00000000..e02cb4bd
--- /dev/null
+++ b/src/test/java/org/ttzero/excel/entity/HideColumnTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2017-2022, 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.entity;
+
+import org.junit.Test;
+import org.ttzero.excel.annotation.ExcelColumn;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * @author wangguanquan3 at 2022-08-03 08:50
+ */
+public class HideColumnTest extends WorkbookTest {
+    @Test public void testColumnHide() throws IOException {
+        Column[] columns = {new Column("ID", "id").hide(), new Column("NAME", "name")};
+        new Workbook("Hide Column").addSheet(new ListSheet<>(ListObjectSheetTest.Item.randomTestData(10), columns))
+            .writeTo(defaultTestPath);
+    }
+
+    @Test public void testColumnAnnoHide() throws IOException {
+        new Workbook("Hide Column Annotation").addSheet(new ListSheet<>(HideColumnItem.randomTestData(10)))
+            .writeTo(defaultTestPath);
+    }
+
+    public static class HideColumnItem extends ListObjectSheetTest.Item {
+        public HideColumnItem() {
+        }
+
+        public HideColumnItem(int id, String name) {
+            super(id, name);
+        }
+
+        @ExcelColumn(hide = true)
+        @Override
+        public int getId() {
+            return super.getId();
+        }
+
+        public static List<ListObjectSheetTest.Item> randomTestData(int n) {
+            return randomTestData(n, () -> new HideColumnItem(random.nextInt(100), getRandomString()));
+        }
+    }
+}
diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
index 0175b7d7..e243406f 100644
--- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
@@ -693,7 +693,7 @@ public class ListObjectSheetTest extends WorkbookTest {
         private int id;
         @ExcelColumn(wrapText = true)
         private String name;
-
+        public Item() { }
         public Item(int id, String name) {
             this.id = id;
             this.name = name;
@@ -706,18 +706,21 @@ public class ListObjectSheetTest extends WorkbookTest {
         public String getName() {
             return name;
         }
-
         public static List<Item> randomTestData(int n) {
+            return randomTestData(n, () -> new Item(random.nextInt(100), getRandomString()));
+        }
+
+        public static List<Item> randomTestData(int n, Supplier<Item> supplier) {
             List<Item> list = new ArrayList<>(n);
             for (int i = 0; i < n; i++) {
-                list.add(new Item(i, getRandomString()));
+                list.add(supplier.get());
             }
             return list;
         }
 
         public static List<Item> randomTestData() {
             int n = random.nextInt(100) + 1;
-            return randomTestData(n);
+            return randomTestData(n, () -> new Item(random.nextInt(100), getRandomString()));
         }
     }