作者 RuoYi

Excel注解支持color字体颜色

@@ -5,6 +5,8 @@ import java.lang.annotation.Retention; @@ -5,6 +5,8 @@ import java.lang.annotation.Retention;
5 import java.lang.annotation.RetentionPolicy; 5 import java.lang.annotation.RetentionPolicy;
6 import java.lang.annotation.Target; 6 import java.lang.annotation.Target;
7 import java.math.BigDecimal; 7 import java.math.BigDecimal;
  8 +import org.apache.poi.ss.usermodel.HorizontalAlignment;
  9 +import org.apache.poi.ss.usermodel.IndexedColors;
8 import com.ruoyi.common.utils.poi.ExcelHandlerAdapter; 10 import com.ruoyi.common.utils.poi.ExcelHandlerAdapter;
9 11
10 /** 12 /**
@@ -57,11 +59,6 @@ public @interface Excel @@ -57,11 +59,6 @@ public @interface Excel
57 public int roundingMode() default BigDecimal.ROUND_HALF_EVEN; 59 public int roundingMode() default BigDecimal.ROUND_HALF_EVEN;
58 60
59 /** 61 /**
60 - * 导出类型(0数字 1字符串)  
61 - */  
62 - public ColumnType cellType() default ColumnType.STRING;  
63 -  
64 - /**  
65 * 导出时在excel中每个列的高度 单位为字符 62 * 导出时在excel中每个列的高度 单位为字符
66 */ 63 */
67 public double height() default 14; 64 public double height() default 14;
@@ -107,9 +104,19 @@ public @interface Excel @@ -107,9 +104,19 @@ public @interface Excel
107 public boolean isStatistics() default false; 104 public boolean isStatistics() default false;
108 105
109 /** 106 /**
110 - * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右) 107 + * 导出类型(0数字 1字符串)
  108 + */
  109 + public ColumnType cellType() default ColumnType.STRING;
  110 +
  111 + /**
  112 + * 导出字体颜色
  113 + */
  114 + public IndexedColors color() default IndexedColors.BLACK;
  115 +
  116 + /**
  117 + * 导出字段对齐方式
111 */ 118 */
112 - public Align align() default Align.AUTO; 119 + public HorizontalAlignment align() default HorizontalAlignment.CENTER;
113 120
114 /** 121 /**
115 * 自定义数据处理器 122 * 自定义数据处理器
@@ -672,21 +672,6 @@ public class ExcelUtil<T> @@ -672,21 +672,6 @@ public class ExcelUtil<T>
672 style.setFont(totalFont); 672 style.setFont(totalFont);
673 styles.put("total", style); 673 styles.put("total", style);
674 674
675 - style = wb.createCellStyle();  
676 - style.cloneStyleFrom(styles.get("data"));  
677 - style.setAlignment(HorizontalAlignment.LEFT);  
678 - styles.put("data1", style);  
679 -  
680 - style = wb.createCellStyle();  
681 - style.cloneStyleFrom(styles.get("data"));  
682 - style.setAlignment(HorizontalAlignment.CENTER);  
683 - styles.put("data2", style);  
684 -  
685 - style = wb.createCellStyle();  
686 - style.cloneStyleFrom(styles.get("data"));  
687 - style.setAlignment(HorizontalAlignment.RIGHT);  
688 - styles.put("data3", style);  
689 -  
690 return styles; 675 return styles;
691 } 676 }
692 677
@@ -808,8 +793,7 @@ public class ExcelUtil<T> @@ -808,8 +793,7 @@ public class ExcelUtil<T>
808 { 793 {
809 // 创建cell 794 // 创建cell
810 cell = row.createCell(column); 795 cell = row.createCell(column);
811 - int align = attr.align().value();  
812 - cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : ""))); 796 + setDataCell(cell, attr);
813 797
814 // 用于读取对象中的属性 798 // 用于读取对象中的属性
815 Object value = getTargetValue(vo, field, attr); 799 Object value = getTargetValue(vo, field, attr);
@@ -853,6 +837,34 @@ public class ExcelUtil<T> @@ -853,6 +837,34 @@ public class ExcelUtil<T>
853 } 837 }
854 838
855 /** 839 /**
  840 + * 设置单元格样式
  841 + *
  842 + * @param cell 单元格
  843 + * @param excel 注解信息
  844 + */
  845 + public void setDataCell(Cell cell, Excel excel)
  846 + {
  847 + CellStyle style = wb.createCellStyle();
  848 + style.setAlignment(HorizontalAlignment.CENTER);
  849 + style.setVerticalAlignment(VerticalAlignment.CENTER);
  850 + style.setBorderRight(BorderStyle.THIN);
  851 + style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  852 + style.setBorderLeft(BorderStyle.THIN);
  853 + style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  854 + style.setBorderTop(BorderStyle.THIN);
  855 + style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  856 + style.setBorderBottom(BorderStyle.THIN);
  857 + style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  858 + style.setAlignment(excel.align());
  859 + Font dataFont = wb.createFont();
  860 + dataFont.setFontName("Arial");
  861 + dataFont.setFontHeightInPoints((short) 10);
  862 + dataFont.setColor(excel.color().index);
  863 + style.setFont(dataFont);
  864 + cell.setCellStyle(style);
  865 + }
  866 +
  867 + /**
856 * 设置 POI XSSFSheet 单元格提示或选择框 868 * 设置 POI XSSFSheet 单元格提示或选择框
857 * 869 *
858 * @param sheet 表单 870 * @param sheet 表单