RuoYi лет назад: 4
Родитель
Сommit
5dbed46552

+ 41 - 29
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -553,6 +553,46 @@ public class ExcelUtil<T>
553
         style.setFont(totalFont);
553
         style.setFont(totalFont);
554
         styles.put("total", style);
554
         styles.put("total", style);
555
 
555
 
556
+        styles.putAll(annotationStyles(wb));
557
+
558
+        return styles;
559
+    }
560
+
561
+    /**
562
+     * 根据Excel注解创建表格样式
563
+     * 
564
+     * @param wb 工作薄对象
565
+     * @return 自定义样式列表
566
+     */
567
+    private Map<String, CellStyle> annotationStyles(Workbook wb)
568
+    {
569
+        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
570
+        for (Object[] os : fields)
571
+        {
572
+            Excel excel = (Excel) os[1];
573
+            String key = "data_" + excel.align() + "_" + excel.color();
574
+            if (!styles.containsKey(key))
575
+            {
576
+                CellStyle style = wb.createCellStyle();
577
+                style = wb.createCellStyle();
578
+                style.setAlignment(excel.align());
579
+                style.setVerticalAlignment(VerticalAlignment.CENTER);
580
+                style.setBorderRight(BorderStyle.THIN);
581
+                style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
582
+                style.setBorderLeft(BorderStyle.THIN);
583
+                style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
584
+                style.setBorderTop(BorderStyle.THIN);
585
+                style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
586
+                style.setBorderBottom(BorderStyle.THIN);
587
+                style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
588
+                Font dataFont = wb.createFont();
589
+                dataFont.setFontName("Arial");
590
+                dataFont.setFontHeightInPoints((short) 10);
591
+                dataFont.setColor(excel.color().index);
592
+                style.setFont(dataFont);
593
+                styles.put(key, style);
594
+            }
595
+        }
556
         return styles;
596
         return styles;
557
     }
597
     }
558
 
598
 
@@ -674,7 +714,7 @@ public class ExcelUtil<T>
674
             {
714
             {
675
                 // 创建cell
715
                 // 创建cell
676
                 cell = row.createCell(column);
716
                 cell = row.createCell(column);
677
-                setDataCell(cell, attr);
717
+                cell.setCellStyle(styles.get("data_" + attr.align() + "_" + attr.color()));
678
 
718
 
679
                 // 用于读取对象中的属性
719
                 // 用于读取对象中的属性
680
                 Object value = getTargetValue(vo, field, attr);
720
                 Object value = getTargetValue(vo, field, attr);
@@ -713,34 +753,6 @@ public class ExcelUtil<T>
713
     }
753
     }
714
 
754
 
715
     /**
755
     /**
716
-     * 设置单元格样式
717
-     * 
718
-     * @param cell 单元格
719
-     * @param excel 注解信息
720
-     */
721
-    public void setDataCell(Cell cell, Excel excel)
722
-    {
723
-        CellStyle style = wb.createCellStyle();
724
-        style.setAlignment(HorizontalAlignment.CENTER);
725
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
726
-        style.setBorderRight(BorderStyle.THIN);
727
-        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
728
-        style.setBorderLeft(BorderStyle.THIN);
729
-        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
730
-        style.setBorderTop(BorderStyle.THIN);
731
-        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
732
-        style.setBorderBottom(BorderStyle.THIN);
733
-        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
734
-        style.setAlignment(excel.align());
735
-        Font dataFont = wb.createFont();
736
-        dataFont.setFontName("Arial");
737
-        dataFont.setFontHeightInPoints((short) 10);
738
-        dataFont.setColor(excel.color().index);
739
-        style.setFont(dataFont);
740
-        cell.setCellStyle(style);
741
-    }
742
-
743
-    /**
744
      * 设置 POI XSSFSheet 单元格提示或选择框
756
      * 设置 POI XSSFSheet 单元格提示或选择框
745
      * 
757
      * 
746
      * @param sheet 表单
758
      * @param sheet 表单