作者 RuoYi

Excel注解支持backgroundColor属性设置背景色

@@ -109,7 +109,22 @@ public @interface Excel @@ -109,7 +109,22 @@ public @interface Excel
109 public ColumnType cellType() default ColumnType.STRING; 109 public ColumnType cellType() default ColumnType.STRING;
110 110
111 /** 111 /**
112 - * 导出字体颜色 112 + * 导出列头背景色
  113 + */
  114 + public IndexedColors headerBackgroundColor() default IndexedColors.GREY_50_PERCENT;
  115 +
  116 + /**
  117 + * 导出列头字体颜色
  118 + */
  119 + public IndexedColors headerColor() default IndexedColors.WHITE;
  120 +
  121 + /**
  122 + * 导出单元格背景色
  123 + */
  124 + public IndexedColors backgroundColor() default IndexedColors.WHITE;
  125 +
  126 + /**
  127 + * 导出单元格字体颜色
113 */ 128 */
114 public IndexedColors color() default IndexedColors.BLACK; 129 public IndexedColors color() default IndexedColors.BLACK;
115 130
@@ -650,20 +650,6 @@ public class ExcelUtil<T> @@ -650,20 +650,6 @@ public class ExcelUtil<T>
650 styles.put("data", style); 650 styles.put("data", style);
651 651
652 style = wb.createCellStyle(); 652 style = wb.createCellStyle();
653 - style.cloneStyleFrom(styles.get("data"));  
654 - style.setAlignment(HorizontalAlignment.CENTER);  
655 - style.setVerticalAlignment(VerticalAlignment.CENTER);  
656 - style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());  
657 - style.setFillPattern(FillPatternType.SOLID_FOREGROUND);  
658 - Font headerFont = wb.createFont();  
659 - headerFont.setFontName("Arial");  
660 - headerFont.setFontHeightInPoints((short) 10);  
661 - headerFont.setBold(true);  
662 - headerFont.setColor(IndexedColors.WHITE.getIndex());  
663 - style.setFont(headerFont);  
664 - styles.put("header", style);  
665 -  
666 - style = wb.createCellStyle();  
667 style.setAlignment(HorizontalAlignment.CENTER); 653 style.setAlignment(HorizontalAlignment.CENTER);
668 style.setVerticalAlignment(VerticalAlignment.CENTER); 654 style.setVerticalAlignment(VerticalAlignment.CENTER);
669 Font totalFont = wb.createFont(); 655 Font totalFont = wb.createFont();
@@ -672,24 +658,60 @@ public class ExcelUtil<T> @@ -672,24 +658,60 @@ public class ExcelUtil<T>
672 style.setFont(totalFont); 658 style.setFont(totalFont);
673 styles.put("total", style); 659 styles.put("total", style);
674 660
675 - styles.putAll(annotationStyles(wb)); 661 + styles.putAll(annotationHeaderStyles(wb, styles));
  662 +
  663 + styles.putAll(annotationDataStyles(wb));
676 664
677 return styles; 665 return styles;
678 } 666 }
679 667
680 /** 668 /**
681 - * 根据Excel注解创建表格样式 669 + * 根据Excel注解创建表格头样式
  670 + *
  671 + * @param wb 工作薄对象
  672 + * @return 自定义样式列表
  673 + */
  674 + private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, CellStyle> styles)
  675 + {
  676 + Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
  677 + for (Object[] os : fields)
  678 + {
  679 + Excel excel = (Excel) os[1];
  680 + String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor());
  681 + if (!headerStyles.containsKey(key))
  682 + {
  683 + CellStyle style = wb.createCellStyle();
  684 + style = wb.createCellStyle();
  685 + style.cloneStyleFrom(styles.get("data"));
  686 + style.setAlignment(HorizontalAlignment.CENTER);
  687 + style.setVerticalAlignment(VerticalAlignment.CENTER);
  688 + style.setFillForegroundColor(excel.headerBackgroundColor().index);
  689 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  690 + Font headerFont = wb.createFont();
  691 + headerFont.setFontName("Arial");
  692 + headerFont.setFontHeightInPoints((short) 10);
  693 + headerFont.setBold(true);
  694 + headerFont.setColor(excel.headerColor().index);
  695 + style.setFont(headerFont);
  696 + headerStyles.put(key, style);
  697 + }
  698 + }
  699 + return headerStyles;
  700 + }
  701 +
  702 + /**
  703 + * 根据Excel注解创建表格列样式
682 * 704 *
683 * @param wb 工作薄对象 705 * @param wb 工作薄对象
684 * @return 自定义样式列表 706 * @return 自定义样式列表
685 */ 707 */
686 - private Map<String, CellStyle> annotationStyles(Workbook wb) 708 + private Map<String, CellStyle> annotationDataStyles(Workbook wb)
687 { 709 {
688 Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); 710 Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
689 for (Object[] os : fields) 711 for (Object[] os : fields)
690 { 712 {
691 Excel excel = (Excel) os[1]; 713 Excel excel = (Excel) os[1];
692 - String key = "data_" + excel.align() + "_" + excel.color(); 714 + String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
693 if (!styles.containsKey(key)) 715 if (!styles.containsKey(key))
694 { 716 {
695 CellStyle style = wb.createCellStyle(); 717 CellStyle style = wb.createCellStyle();
@@ -704,6 +726,8 @@ public class ExcelUtil<T> @@ -704,6 +726,8 @@ public class ExcelUtil<T>
704 style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); 726 style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
705 style.setBorderBottom(BorderStyle.THIN); 727 style.setBorderBottom(BorderStyle.THIN);
706 style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); 728 style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  729 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  730 + style.setFillForegroundColor(excel.backgroundColor().getIndex());
707 Font dataFont = wb.createFont(); 731 Font dataFont = wb.createFont();
708 dataFont.setFontName("Arial"); 732 dataFont.setFontName("Arial");
709 dataFont.setFontHeightInPoints((short) 10); 733 dataFont.setFontHeightInPoints((short) 10);
@@ -725,7 +749,7 @@ public class ExcelUtil<T> @@ -725,7 +749,7 @@ public class ExcelUtil<T>
725 // 写入列信息 749 // 写入列信息
726 cell.setCellValue(attr.name()); 750 cell.setCellValue(attr.name());
727 setDataValidation(attr, row, column); 751 setDataValidation(attr, row, column);
728 - cell.setCellStyle(styles.get("header")); 752 + cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
729 return cell; 753 return cell;
730 } 754 }
731 755
@@ -833,7 +857,7 @@ public class ExcelUtil<T> @@ -833,7 +857,7 @@ public class ExcelUtil<T>
833 { 857 {
834 // 创建cell 858 // 创建cell
835 cell = row.createCell(column); 859 cell = row.createCell(column);
836 - cell.setCellStyle(styles.get("data_" + attr.align() + "_" + attr.color())); 860 + cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
837 861
838 // 用于读取对象中的属性 862 // 用于读取对象中的属性
839 Object value = getTargetValue(vo, field, attr); 863 Object value = getTargetValue(vo, field, attr);