Repository: wangguanquan/eec
Pull request #179 (2021-05-10 12:28:08)

feature: 添加自定义表头样式
添加 @HeaderStyle 注解，目前只支持颜色设置


Patch:
diff --git a/src/main/java/org/ttzero/excel/annotation/HeaderStyle.java b/src/main/java/org/ttzero/excel/annotation/HeaderStyle.java
new file mode 100644
index 00000000..8314c657
--- /dev/null
+++ b/src/main/java/org/ttzero/excel/annotation/HeaderStyle.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2017-2020, 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.annotation;
+
+import org.ttzero.excel.entity.style.Fill;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Custom header styles
+ *
+ * @author jialei
+ * @date 2021-05-10 17:38
+ */
+@Target({ElementType.FIELD, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface HeaderStyle {
+
+    /**
+     * Set font color.
+     *
+     */
+    String fontColor() default "#ffffff";
+
+    /**
+     * Set the background fill color.
+     *
+     * @see Fill#parse(java.lang.String)
+     *
+     */
+    String fillBgColor() default "#666699";
+
+}
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index 9a99478c..b5c56c3d 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -18,9 +18,17 @@ 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.entity.style.Border;
+import org.ttzero.excel.entity.style.Fill;
+import org.ttzero.excel.entity.style.Font;
+import org.ttzero.excel.entity.style.Horizontals;
+import org.ttzero.excel.entity.style.Styles;
+import org.ttzero.excel.entity.style.Verticals;
 import org.ttzero.excel.reader.Cell;
 import org.ttzero.excel.annotation.IgnoreExport;
 
+import java.awt.*;
 import java.beans.IntrospectionException;
 import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
@@ -525,6 +533,11 @@ public class ListSheet<T> extends Sheet {
         ExcelColumn ec = ao.getAnnotation(ExcelColumn.class);
         if (ec != null) {
             Column column = new Column(ec.value(), EMPTY, ec.share());
+            // 解析自定义头样式
+            HeaderStyle hs = ao.getAnnotation(HeaderStyle.class);
+            if (hs != null) {
+                column.setHeaderStyle(this.buildHeadStyle(hs.fontColor(), hs.fillBgColor()));
+            }
             // Comment
             column.headerComment = createComment(ao.getAnnotation(HeaderComment.class), ec.comment());
             // Number format
diff --git a/src/main/java/org/ttzero/excel/entity/Sheet.java b/src/main/java/org/ttzero/excel/entity/Sheet.java
index eb7465be..72452a4f 100644
--- a/src/main/java/org/ttzero/excel/entity/Sheet.java
+++ b/src/main/java/org/ttzero/excel/entity/Sheet.java
@@ -251,6 +251,10 @@ public abstract class Sheet implements Cloneable, Storable {
          * The style of cell, -1 if not be setting
          */
         public int cellStyle = -1;
+        /**
+         * The style of header, -1 if not be setting
+         */
+        public int headerStyle = -1;
         /**
          * The cell width
          */
@@ -554,6 +558,17 @@ public abstract class Sheet implements Cloneable, Storable {
             return this;
         }
 
+        /**
+         * Setting the header's style
+         *
+         * @param headerStyle the styles value
+         * @return the {@link Sheet.Column}
+         */
+        public Column setHeaderStyle(int headerStyle) {
+            this.headerStyle = headerStyle;
+            return this;
+        }
+
         /**
          * Returns the default horizontal style
          * the Date, Character, Bool has center value, the
@@ -1274,20 +1289,32 @@ public abstract class Sheet implements Cloneable, Storable {
         return this;
     }
 
-    public int defaultHeadStyle() {
+    /**
+     * Custom header style according to parameters
+     *
+     * @param fontColor
+     * @param fillBgColor
+     * @return headStyle
+     */
+    public int buildHeadStyle(String fontColor, String fillBgColor) {
         if (headStyle == 0) {
             Styles styles = workbook.getStyles();
             Font font = new Font(workbook.getI18N().getOrElse("local-font-family", "Arial")
-                , 12, Font.Style.BOLD, Color.white);
+                    , 12, Font.Style.BOLD, Color.decode(fontColor));
             headStyle = styles.of(styles.addFont(font)
-                | styles.addFill(Fill.parse("solid #666699"))
-                | styles.addBorder(Border.parse("thin black"))
-                | Verticals.CENTER
-                | Horizontals.CENTER);
+                    | styles.addFill(Fill.parse(String.join(" ",
+                    fillBgColor, "solid")))
+                    | styles.addBorder(Border.parse("thin black"))
+                    | Verticals.CENTER
+                    | Horizontals.CENTER);
         }
         return headStyle;
     }
 
+    public int defaultHeadStyle() {
+        return this.buildHeadStyle("#ffffff", "#666699");
+    }
+
     protected static boolean nonOrIntDefault(int style) {
         return style == -1
             || style == Styles.defaultIntBorderStyle()
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 41705164..b59ce1a1 100644
--- a/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
+++ b/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
@@ -394,11 +394,11 @@ public class XMLWorksheetWriter implements IWorksheetWriter {
 
         if (sheet.isAutoSize()) {
             for (Sheet.Column hc : columns) {
-                writeStringAutoSize(isNotEmpty(hc.getName()) ? hc.getName() : hc.key, row, c++, defaultStyle);
+                writeStringAutoSize(isNotEmpty(hc.getName()) ? hc.getName() : hc.key, row, c++, hc.headerStyle == -1 ? defaultStyle : hc.headerStyle);
             }
         } else {
             for (Sheet.Column hc : columns) {
-                writeString(isNotEmpty(hc.getName()) ? hc.getName() : hc.key, row, c++, defaultStyle);
+                writeString(isNotEmpty(hc.getName()) ? hc.getName() : hc.key, row, c++, hc.headerStyle == -1 ? defaultStyle : hc.headerStyle);
             }
         }
 


Test patch:
diff --git a/src/test/java/org/ttzero/excel/annotation/HeaderStyleTest.java b/src/test/java/org/ttzero/excel/annotation/HeaderStyleTest.java
new file mode 100644
index 00000000..ba0c492a
--- /dev/null
+++ b/src/test/java/org/ttzero/excel/annotation/HeaderStyleTest.java
@@ -0,0 +1,68 @@
+package org.ttzero.excel.annotation;
+
+import org.junit.Test;
+import org.ttzero.excel.entity.ListSheet;
+import org.ttzero.excel.entity.Workbook;
+import org.ttzero.excel.entity.WorkbookTest;
+
+import java.io.IOException;
+import java.util.Collections;
+
+/**
+ * 自定义导出Excel表头样式
+ */
+public class HeaderStyleTest extends WorkbookTest {
+
+    @Test
+    public void originalTest() throws IOException {
+        Head itemFull = new Head();
+        new Workbook("customize_header_style_original").setAutoSize(true).addSheet(new ListSheet<>(Collections.singletonList(itemFull))).writeTo(defaultTestPath);
+
+    }
+
+    @Test
+    public void fillBgColorTest() throws IOException {
+        Head1 itemFull = new Head1();
+        new Workbook("customize_header_style_bgc").setAutoSize(true).addSheet(new ListSheet<>(Collections.singletonList(itemFull))).writeTo(defaultTestPath);
+
+    }
+
+    @Test
+    public void fontColorTest() throws IOException {
+        Head2 itemFull = new Head2();
+        new Workbook("customize_header_style_fc").setAutoSize(true).addSheet(new ListSheet<>(Collections.singletonList(itemFull))).writeTo(defaultTestPath);
+
+    }
+
+    static class Head {
+
+        /**
+         * errorMsg
+         */
+        @ExcelColumn(value = "错误信息")
+        private String errorMsg;
+
+    }
+
+    static class Head1 {
+
+        /**
+         * errorMsg
+         */
+        @ExcelColumn(value = "错误信息")
+        @HeaderStyle(fillBgColor = "#ff0000")
+        private String errorMsg;
+
+    }
+
+    static class Head2 {
+
+        /**
+         * errorMsg
+         */
+        @ExcelColumn(value = "错误信息")
+        @HeaderStyle(fontColor = "#ff0000")
+        private String errorMsg;
+
+    }
+}
diff --git a/src/test/java/org/ttzero/excel/entity/WorkbookTest.java b/src/test/java/org/ttzero/excel/entity/WorkbookTest.java
index f7e480f0..a086af8b 100644
--- a/src/test/java/org/ttzero/excel/entity/WorkbookTest.java
+++ b/src/test/java/org/ttzero/excel/entity/WorkbookTest.java
@@ -32,7 +32,7 @@ public class WorkbookTest {
     /**
      * The default output path
      */
-    static Path defaultTestPath = Paths.get("target/excel/");
+    public static Path defaultTestPath = Paths.get("target/excel/");
     public static String author = "guanquan.wang";
     public static Random random = new Random();