| | |
| | | import java.util.UUID;
|
| | | import java.util.stream.Collectors;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import org.apache.commons.lang3.ArrayUtils;
|
| | | import org.apache.commons.lang3.RegExUtils;
|
| | | import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
|
| | | import org.apache.poi.hssf.usermodel.HSSFPicture;
|
| | |
| | | */
|
| | | public Class<T> clazz;
|
| | |
|
| | | /**
|
| | | * 需要排除列属性
|
| | | */
|
| | | public String[] excludeFields;
|
| | |
|
| | | public ExcelUtil(Class<T> clazz)
|
| | | {
|
| | | this.clazz = clazz;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 隐藏Excel中列属性
|
| | | *
|
| | | * @param fields 列属性名 示例[单个"name"/多个"id","name"]
|
| | | * @throws Exception
|
| | | */
|
| | | public void hideColumn(String... fields)
|
| | | {
|
| | | this.excludeFields = fields;
|
| | | }
|
| | |
|
| | | public void init(List<T> list, String sheetName, String title, Type type)
|
| | |
| | | styles.put("data", style);
|
| | |
|
| | | style = wb.createCellStyle();
|
| | | style.cloneStyleFrom(styles.get("data"));
|
| | | style.setAlignment(HorizontalAlignment.CENTER);
|
| | | style.setVerticalAlignment(VerticalAlignment.CENTER);
|
| | | style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
| | | Font headerFont = wb.createFont();
|
| | | headerFont.setFontName("Arial");
|
| | | headerFont.setFontHeightInPoints((short) 10);
|
| | | headerFont.setBold(true);
|
| | | headerFont.setColor(IndexedColors.WHITE.getIndex());
|
| | | style.setFont(headerFont);
|
| | | styles.put("header", style);
|
| | |
|
| | | style = wb.createCellStyle();
|
| | | style.setAlignment(HorizontalAlignment.CENTER);
|
| | | style.setVerticalAlignment(VerticalAlignment.CENTER);
|
| | | Font totalFont = wb.createFont();
|
| | |
| | | style.setFont(totalFont);
|
| | | styles.put("total", style);
|
| | |
|
| | | styles.putAll(annotationHeaderStyles(wb, styles));
|
| | |
|
| | | styles.putAll(annotationDataStyles(wb));
|
| | |
|
| | | return styles;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据Excel注解创建表格头样式
|
| | | * |
| | | * @param wb 工作薄对象
|
| | | * @return 自定义样式列表
|
| | | */
|
| | | private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, CellStyle> styles)
|
| | | {
|
| | | Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
|
| | | for (Object[] os : fields)
|
| | | {
|
| | | Excel excel = (Excel) os[1];
|
| | | String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor());
|
| | | if (!headerStyles.containsKey(key))
|
| | | {
|
| | | CellStyle style = wb.createCellStyle();
|
| | | style = wb.createCellStyle();
|
| | | style.cloneStyleFrom(styles.get("data"));
|
| | | style.setAlignment(HorizontalAlignment.CENTER);
|
| | | style.setVerticalAlignment(VerticalAlignment.CENTER);
|
| | | style.setFillForegroundColor(excel.headerBackgroundColor().index);
|
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
| | | Font headerFont = wb.createFont();
|
| | | headerFont.setFontName("Arial");
|
| | | headerFont.setFontHeightInPoints((short) 10);
|
| | | headerFont.setBold(true);
|
| | | headerFont.setColor(excel.headerColor().index);
|
| | | style.setFont(headerFont);
|
| | | headerStyles.put(key, style);
|
| | | }
|
| | | }
|
| | | return headerStyles;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据Excel注解创建表格列样式
|
| | | * |
| | | * @param wb 工作薄对象
|
| | | * @return 自定义样式列表
|
| | | */
|
| | | private Map<String, CellStyle> annotationDataStyles(Workbook wb)
|
| | | {
|
| | | Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
|
| | | for (Object[] os : fields)
|
| | | {
|
| | | Excel excel = (Excel) os[1];
|
| | | String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
|
| | | if (!styles.containsKey(key))
|
| | | {
|
| | | CellStyle style = wb.createCellStyle();
|
| | | style = wb.createCellStyle();
|
| | | style.setAlignment(excel.align());
|
| | | style.setVerticalAlignment(VerticalAlignment.CENTER);
|
| | | style.setBorderRight(BorderStyle.THIN);
|
| | | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setBorderLeft(BorderStyle.THIN);
|
| | | style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setBorderTop(BorderStyle.THIN);
|
| | | style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setBorderBottom(BorderStyle.THIN);
|
| | | style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
| | | style.setFillForegroundColor(excel.backgroundColor().getIndex());
|
| | | Font dataFont = wb.createFont();
|
| | | dataFont.setFontName("Arial");
|
| | | dataFont.setFontHeightInPoints((short) 10);
|
| | | dataFont.setColor(excel.color().index);
|
| | | style.setFont(dataFont);
|
| | | styles.put(key, style);
|
| | | }
|
| | | }
|
| | | return styles;
|
| | | }
|
| | |
|
| | |
| | | // 写入列信息
|
| | | cell.setCellValue(attr.name());
|
| | | setDataValidation(attr, row, column);
|
| | | cell.setCellStyle(styles.get("header"));
|
| | | cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
|
| | | return cell;
|
| | | }
|
| | |
|
| | |
| | | {
|
| | | // 创建cell
|
| | | cell = row.createCell(column);
|
| | | setDataCell(cell, attr);
|
| | | cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
|
| | |
|
| | | // 用于读取对象中的属性
|
| | | Object value = getTargetValue(vo, field, attr);
|
| | |
| | | log.error("导出Excel失败{}", e);
|
| | | }
|
| | | return cell;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 设置单元格样式
|
| | | * |
| | | * @param cell 单元格
|
| | | * @param excel 注解信息
|
| | | */
|
| | | public void setDataCell(Cell cell, Excel excel)
|
| | | {
|
| | | CellStyle style = wb.createCellStyle();
|
| | | style.setAlignment(HorizontalAlignment.CENTER);
|
| | | style.setVerticalAlignment(VerticalAlignment.CENTER);
|
| | | style.setBorderRight(BorderStyle.THIN);
|
| | | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setBorderLeft(BorderStyle.THIN);
|
| | | style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setBorderTop(BorderStyle.THIN);
|
| | | style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setBorderBottom(BorderStyle.THIN);
|
| | | style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
| | | style.setAlignment(excel.align());
|
| | | Font dataFont = wb.createFont();
|
| | | dataFont.setFontName("Arial");
|
| | | dataFont.setFontHeightInPoints((short) 10);
|
| | | dataFont.setColor(excel.color().index);
|
| | | style.setFont(dataFont);
|
| | | cell.setCellStyle(style);
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
| | | for (Field field : tempFields)
|
| | | {
|
| | | // 单注解
|
| | | if (field.isAnnotationPresent(Excel.class))
|
| | | if (!ArrayUtils.contains(this.excludeFields, field.getName()))
|
| | | {
|
| | | Excel attr = field.getAnnotation(Excel.class);
|
| | | if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
| | | // 单注解
|
| | | if (field.isAnnotationPresent(Excel.class))
|
| | | {
|
| | | field.setAccessible(true);
|
| | | fields.add(new Object[] { field, attr });
|
| | | }
|
| | | }
|
| | |
|
| | | // 多注解
|
| | | if (field.isAnnotationPresent(Excels.class))
|
| | | {
|
| | | Excels attrs = field.getAnnotation(Excels.class);
|
| | | Excel[] excels = attrs.value();
|
| | | for (Excel attr : excels)
|
| | | {
|
| | | Excel attr = field.getAnnotation(Excel.class);
|
| | | if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
| | | {
|
| | | field.setAccessible(true);
|
| | | fields.add(new Object[] { field, attr });
|
| | | }
|
| | | }
|
| | |
|
| | | // 多注解
|
| | | if (field.isAnnotationPresent(Excels.class))
|
| | | {
|
| | | Excels attrs = field.getAnnotation(Excels.class);
|
| | | Excel[] excels = attrs.value();
|
| | | for (Excel attr : excels)
|
| | | {
|
| | | if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
|
| | | {
|
| | | field.setAccessible(true);
|
| | | fields.add(new Object[] { field, attr });
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | return fields;
|