作者 RuoYi

优化excel创建表格样式

@@ -672,6 +672,46 @@ public class ExcelUtil<T> @@ -672,6 +672,46 @@ 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 + styles.putAll(annotationStyles(wb));
  676 +
  677 + return styles;
  678 + }
  679 +
  680 + /**
  681 + * 根据Excel注解创建表格样式
  682 + *
  683 + * @param wb 工作薄对象
  684 + * @return 自定义样式列表
  685 + */
  686 + private Map<String, CellStyle> annotationStyles(Workbook wb)
  687 + {
  688 + Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
  689 + for (Object[] os : fields)
  690 + {
  691 + Excel excel = (Excel) os[1];
  692 + String key = "data_" + excel.align() + "_" + excel.color();
  693 + if (!styles.containsKey(key))
  694 + {
  695 + CellStyle style = wb.createCellStyle();
  696 + style = wb.createCellStyle();
  697 + style.setAlignment(excel.align());
  698 + style.setVerticalAlignment(VerticalAlignment.CENTER);
  699 + style.setBorderRight(BorderStyle.THIN);
  700 + style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  701 + style.setBorderLeft(BorderStyle.THIN);
  702 + style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  703 + style.setBorderTop(BorderStyle.THIN);
  704 + style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  705 + style.setBorderBottom(BorderStyle.THIN);
  706 + style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  707 + Font dataFont = wb.createFont();
  708 + dataFont.setFontName("Arial");
  709 + dataFont.setFontHeightInPoints((short) 10);
  710 + dataFont.setColor(excel.color().index);
  711 + style.setFont(dataFont);
  712 + styles.put(key, style);
  713 + }
  714 + }
675 return styles; 715 return styles;
676 } 716 }
677 717
@@ -793,7 +833,7 @@ public class ExcelUtil<T> @@ -793,7 +833,7 @@ public class ExcelUtil<T>
793 { 833 {
794 // 创建cell 834 // 创建cell
795 cell = row.createCell(column); 835 cell = row.createCell(column);
796 - setDataCell(cell, attr); 836 + cell.setCellStyle(styles.get("data_" + attr.align() + "_" + attr.color()));
797 837
798 // 用于读取对象中的属性 838 // 用于读取对象中的属性
799 Object value = getTargetValue(vo, field, attr); 839 Object value = getTargetValue(vo, field, attr);
@@ -837,34 +877,6 @@ public class ExcelUtil<T> @@ -837,34 +877,6 @@ public class ExcelUtil<T>
837 } 877 }
838 878
839 /** 879 /**
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 - /**  
868 * 设置 POI XSSFSheet 单元格提示或选择框 880 * 设置 POI XSSFSheet 单元格提示或选择框
869 * 881 *
870 * @param sheet 表单 882 * @param sheet 表单