Repository: wangguanquan/eec
Pull request #236 (2022-03-13 04:31:32)

标识行样式
对校验错误行做整行的背景色设置.
硬改出来的,写得不太优雅.作者要是能优化一版就好了


Patch:
diff --git a/src/main/java/org/ttzero/excel/annotation/StyleDesign.java b/src/main/java/org/ttzero/excel/annotation/StyleDesign.java
new file mode 100644
index 00000000..2441fa68
--- /dev/null
+++ b/src/main/java/org/ttzero/excel/annotation/StyleDesign.java
@@ -0,0 +1,23 @@
+package org.ttzero.excel.annotation;
+
+import org.ttzero.excel.processor.StyleProcessor;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * customer style of specified row
+ *
+ * @author suyl at 2022-03-23 17:38
+ *
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+public @interface StyleDesign {
+    Class<? extends StyleProcessor> using() default StyleProcessor.None.class;
+}
+
diff --git a/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java b/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
index a611fe34..cd83e82e 100644
--- a/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
+++ b/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
@@ -16,6 +16,7 @@
 
 package org.ttzero.excel.entity;
 
+import org.ttzero.excel.processor.StyleProcessor;
 import org.ttzero.excel.reader.Cell;
 import org.ttzero.excel.util.DateUtil;
 
@@ -69,6 +70,17 @@ public interface ICellValueAndStyle {
      */
     int getStyleIndex(int rows, Column hc, Object o);
 
+    /**
+     * Setting all cell style of the specified row
+     *
+     * @param <T> the row's class
+     * @param o the row data
+     * @param cell the cell of row
+     * @param hc the header column
+     * @param styleProcessor the styleProcessor
+     */
+    <T> void setStyleDesign(T o, Cell cell, Column hc, StyleProcessor styleProcessor);
+
     /**
      * Setting cell value
      *
diff --git a/src/main/java/org/ttzero/excel/entity/ListMapSheet.java b/src/main/java/org/ttzero/excel/entity/ListMapSheet.java
index 9598ea33..ac1e93d3 100644
--- a/src/main/java/org/ttzero/excel/entity/ListMapSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListMapSheet.java
@@ -159,6 +159,7 @@ public class ListMapSheet extends ListSheet<Map<String, ?>> {
                 cell.clear();
 
                 cellValueAndStyle.reset(rows, cell, e, hc);
+                cellValueAndStyle.setStyleDesign(data.get(start),cell,columns[i],getStyleProcessor());
             }
         }
     }
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index c7e1f001..793ab94c 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -19,9 +19,13 @@ 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.annotation.IgnoreExport;
+import org.ttzero.excel.annotation.StyleDesign;
+import org.ttzero.excel.entity.style.Styles;
 import org.ttzero.excel.processor.ConversionProcessor;
+import org.ttzero.excel.processor.StyleProcessor;
 import org.ttzero.excel.reader.Cell;
-import org.ttzero.excel.annotation.IgnoreExport;
+import org.ttzero.excel.reader.UncheckedTypeException;
 
 import java.beans.IntrospectionException;
 import java.beans.Introspector;
@@ -41,12 +45,11 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-
 import static org.ttzero.excel.util.ReflectUtil.listDeclaredFields;
 import static org.ttzero.excel.util.ReflectUtil.listReadMethods;
 import static org.ttzero.excel.util.StringUtil.EMPTY;
-import static org.ttzero.excel.util.StringUtil.isNotEmpty;
 import static org.ttzero.excel.util.StringUtil.isEmpty;
+import static org.ttzero.excel.util.StringUtil.isNotEmpty;
 
 /**
  * List is the most important data source, you can pass all
@@ -66,6 +69,21 @@ public class ListSheet<T> extends Sheet {
     protected boolean eof;
     private int size;
 
+    /**
+     * The custom styleProcessor
+     */
+    protected StyleProcessor styleProcessor;
+
+    public Sheet setStyleProcessor(StyleProcessor styleProcessor) {
+        this.styleProcessor = styleProcessor;
+        return this;
+    }
+
+    public StyleProcessor getStyleProcessor() {
+        return this.styleProcessor;
+    }
+
+
     /**
      * Constructor worksheet
      */
@@ -123,6 +141,7 @@ public class ListSheet<T> extends Sheet {
         setData(data);
     }
 
+
     /**
      * Constructor worksheet
      *
@@ -233,6 +252,7 @@ public class ListSheet<T> extends Sheet {
         super.close();
     }
 
+
     /**
      * Reset the row-block data
      */
@@ -267,6 +287,7 @@ public class ListSheet<T> extends Sheet {
                     else e = o;
 
                     cellValueAndStyle.reset(rows, cell, e, columns[i]);
+                    cellValueAndStyle.setStyleDesign(o,cell,columns[i],getStyleProcessor());
                 }
             }
         } catch (IllegalAccessException | InvocationTargetException e) {
@@ -498,7 +519,7 @@ public class ListSheet<T> extends Sheet {
 
         // Merge Header Style defined on Entry Class
         mergeGlobalSetting(clazz);
-
+        setDesignStyle(clazz);
         return columns.length;
     }
 
@@ -599,6 +620,24 @@ public class ListSheet<T> extends Sheet {
         }
     }
 
+    /**
+     * Set custom styleProcessor for declarations on Entry Class
+     * @param clazz  Class of &lt;T&gt;
+     */
+    protected void setDesignStyle(Class<?> clazz) {
+        Styles styles = workbook.getStyles();
+        if(null != styles) {
+            StyleDesign designStyle = clazz.getDeclaredAnnotation(StyleDesign.class);
+            if(designStyle != null) {
+                try {
+                    setStyleProcessor(designStyle.using().newInstance());
+                } catch (InstantiationException |IllegalAccessException e) {
+                    throw new UncheckedTypeException(designStyle + " new instance error.", e);
+                }
+            }
+        }
+    }
+
     /**
      * Ignore some columns, override this method to add custom filtering
      *
diff --git a/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java b/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
index 87dc15c7..46b2c1b0 100644
--- a/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
@@ -231,6 +231,7 @@ public class ResultSetSheet extends Sheet {
                     }
 
                     cellValueAndStyle.reset(rows, cell, e, hc);
+                    //setStyleDesign(rs,cell,hc);
                 }
             }
         } catch (SQLException e) {
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 e93708f3..49e788ff 100644
--- a/src/main/java/org/ttzero/excel/entity/csv/CSVCellValueAndStyle.java
+++ b/src/main/java/org/ttzero/excel/entity/csv/CSVCellValueAndStyle.java
@@ -19,6 +19,7 @@ package org.ttzero.excel.entity.csv;
 import org.ttzero.excel.entity.Column;
 import org.ttzero.excel.entity.ICellValueAndStyle;
 import org.ttzero.excel.manager.Const;
+import org.ttzero.excel.processor.StyleProcessor;
 import org.ttzero.excel.reader.Cell;
 import org.ttzero.excel.util.DateUtil;
 
@@ -58,6 +59,20 @@ public class CSVCellValueAndStyle implements ICellValueAndStyle {
         setCellValue(row, cell, e, hc, hc.getClazz(), hc.processor != null);
     }
 
+    /**
+     * Setting all cell style of the specified row
+     *
+     * @param <T> the row's class
+     * @param o the row data
+     * @param cell the cell of row
+     * @param hc the header column
+     * @param styleProcessor the styleProcessor
+     */
+    @Override
+    public <T> void setStyleDesign(T o, Cell cell, Column hc, StyleProcessor styleProcessor) {
+
+    }
+
     /**
      * Returns the worksheet name
      *
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 752b8822..9329113c 100644
--- a/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java
+++ b/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java
@@ -20,6 +20,7 @@ import org.ttzero.excel.entity.Column;
 import org.ttzero.excel.entity.ICellValueAndStyle;
 import org.ttzero.excel.entity.style.Styles;
 import org.ttzero.excel.manager.Const;
+import org.ttzero.excel.processor.StyleProcessor;
 import org.ttzero.excel.reader.Cell;
 
 import static org.ttzero.excel.entity.IWorksheetWriter.isChar;
@@ -126,6 +127,23 @@ public class XMLCellValueAndStyle implements ICellValueAndStyle {
         return getStyleIndex(rows, hc, o, style);
     }
 
+    /**
+     * Setting all cell style of the specified row
+     *
+     * @param <T> the row's class
+     * @param o the row data
+     * @param cell the cell of row
+     * @param hc the header column
+     * @param styleProcessor the styleProcessor
+     */
+    @Override
+    public <T> void setStyleDesign(T o, Cell cell, Column hc, StyleProcessor styleProcessor) {
+        Styles styles = hc.styles;
+        if(null != styles && null != styleProcessor) {
+            cell.xf = styles.of(styleProcessor.build(o,hc.cellStyle,styles));
+        }
+    }
+
     /**
      * Check the odd rows
      *
diff --git a/src/main/java/org/ttzero/excel/processor/StyleProcessor.java b/src/main/java/org/ttzero/excel/processor/StyleProcessor.java
index 89f7764a..de6cdc3c 100644
--- a/src/main/java/org/ttzero/excel/processor/StyleProcessor.java
+++ b/src/main/java/org/ttzero/excel/processor/StyleProcessor.java
@@ -18,13 +18,14 @@ package org.ttzero.excel.processor;
 
 import org.ttzero.excel.entity.style.Styles;
 
+
 /**
  * The style conversion
  *
  * @author guanquan.wang on 2017/10/13.
  */
 @FunctionalInterface
-public interface StyleProcessor {
+public interface StyleProcessor<T> {
     /**
      * The style conversion
      * You must add it using {@code Styles#addXXX} method before adding a style,
@@ -39,5 +40,13 @@ public interface StyleProcessor {
      * @param sst   the {@link Styles} entry
      * @return new style of cell
      */
-    int build(Object o, int style, Styles sst);
+    int build(T o, int style, Styles sst);
+
+    class None implements StyleProcessor<Object> {
+
+        @Override
+        public int build(Object o, int style, Styles sst) {
+            return style;
+        }
+    }
 }


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 f5dafa76..6d88c10a 100644
--- a/src/test/java/org/ttzero/excel/entity/ListMapSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListMapSheetTest.java
@@ -18,7 +18,11 @@ package org.ttzero.excel.entity;
 
 import org.junit.Test;
 import org.ttzero.excel.Print;
+import org.ttzero.excel.entity.style.Fill;
+import org.ttzero.excel.entity.style.PatternType;
+import org.ttzero.excel.entity.style.Styles;
 
+import java.awt.Color;
 import java.io.IOException;
 import java.math.BigDecimal;
 import java.sql.Time;
@@ -56,6 +60,20 @@ public class ListMapSheetTest extends WorkbookTest {
             .writeTo(defaultTestPath);
     }
 
+    @Test public void testStyleDesign4Map() throws IOException {
+        new Workbook("Map标识行样式", author)
+                .addSheet(new ListMapSheet("Map", createAllTypeData(10)).setStyleProcessor((map, style, sst)->{
+                    if(map instanceof Map){
+                        Map<String, Object> map1 = (Map<String, Object>) map;
+                        if ((Boolean) map1.get("bv")) {
+                            style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.green));
+                        }
+                    }
+                    return style;
+                }))
+                .writeTo(defaultTestPath);
+    }
+
     @Test public void testHeaderColumn() throws IOException {
         new Workbook("test header column map", author)
             .watch(Print::println)
diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
index 6859070a..2e090214 100644
--- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
@@ -20,6 +20,7 @@ import org.junit.Test;
 import org.ttzero.excel.Print;
 import org.ttzero.excel.annotation.ExcelColumn;
 import org.ttzero.excel.annotation.IgnoreExport;
+import org.ttzero.excel.annotation.StyleDesign;
 import org.ttzero.excel.entity.style.Fill;
 import org.ttzero.excel.entity.style.Font;
 import org.ttzero.excel.entity.style.PatternType;
@@ -317,7 +318,7 @@ public class ListObjectSheetTest extends WorkbookTest {
     public static StyleProcessor sp = (o, style, sst) -> {
         if ((int)o < 60) {
             style = Styles.clearFill(style)
-                | sst.addFill(new Fill(PatternType.solid, Color.orange));
+                | sst.addFill(new Fill(PatternType.solid,Color.green, Color.blue));
         }
         return style;
     };
@@ -682,6 +683,65 @@ public class ListObjectSheetTest extends WorkbookTest {
         }
     }
 
+
+    @Test public void testStyleDesign() throws IOException {
+        new Workbook("标识行样式", author)
+                .addSheet(new ListSheet<Student>("期末成绩",Student.randomTestData()))
+                .writeTo(defaultTestPath);
+    }
+
+    @Test public void testStyleDesign1() throws IOException {
+        ListSheet itemListSheet = new ListSheet<Item>("序列数",Item.randomTestData());
+        itemListSheet.setStyleProcessor(rainbowStyle);
+        new Workbook("标识行样式1", author)
+                .addSheet(itemListSheet)
+                .writeTo(defaultTestPath);
+    }
+
+    @Test public void testStyleDesign2() throws IOException {
+        new Workbook("标识行样式2", author)
+                .addSheet(new ListSheet<Item>("序列数",Item.randomTestData()).setStyleProcessor((item,style,sst)->{
+                    if(item instanceof Item){
+                        Item item1 = (Item)item;
+                        if (item1.getId() < 10) {
+                            style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.green));
+                        }
+                    }
+                    return style;
+                }))
+                .writeTo(defaultTestPath);
+    }
+
+
+    public static class StudentScoreStyle implements StyleProcessor<Student> {
+        @Override
+        public int build(Student o, int style, Styles sst) {
+            // 低于60分时背景色标黄
+            if (o.getScore() < 60) {
+                style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.orange));
+                // 低于30分时加下划线
+            }else if (o.getScore() < 70) {
+                style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.green));
+            }else if (o.getScore() > 90) {
+                // 获取原有字体+下划线（这样做可以保留原字体和大小）
+                Font newFont = sst.getFont(style).clone();
+                style = Styles.clearFont(style) | sst.addFont(newFont.underLine().bold());
+            }
+            return style;
+        }
+    }
+
+    public static StyleProcessor<Item> rainbowStyle = (item, style, sst)->{
+        if (item.getId()%3==0) {
+            style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.green));
+        }else if (item.getId()%3==1) {
+            style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.blue));
+        }else if (item.getId()%3==2) {
+            style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.pink));
+        }
+        return style;
+    };
+
     public static class Item {
         @ExcelColumn
         private int id;
@@ -907,6 +967,8 @@ public class ListObjectSheetTest extends WorkbookTest {
     /**
      * Annotation Object
      */
+    @StyleDesign(using = StudentScoreStyle.class)
+    //@WaringStyle(fillFgColor = "yellow")
     public static class Student {
         @IgnoreExport
         private int id;
diff --git a/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java b/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java
index 0f6772c3..a5988acd 100644
--- a/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java
@@ -50,6 +50,26 @@ public class ResultSetSheetTest extends SQLWorkbookTest {
         }
     }
 
+//    @Test public void testStyleDesign4RS() throws IOException, SQLException {
+//        try (
+//                Connection con = getConnection();
+//                PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10");
+//                ResultSet rs = ps.executeQuery()
+//        ) {
+//            new Workbook("test ResultSet sheet Constructor2", author)
+//                    .addSheet(new ResultSetSheet().setRs(rs).setStyleProcessor((rset, style, sst)->{
+//                        if(rset instanceof ResultSet){
+//                            ResultSet rset1 = (ResultSet) rset;
+//                            if (rset1.getInt("age")>14) {
+//                                style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.green));
+//                            }
+//                        }
+//                        return style;
+//                    }))
+//                    .writeTo(defaultTestPath);
+//        }
+//    }
+
     @Test public void testConstructor1() throws IOException {
         try {
             new Workbook("test ResultSet sheet Constructor1", author)