| | |
| | | |
| | | // 创建行并设置高度 |
| | | row = sheet.createRow(0); |
| | | row.setHeight((short)(40 * 40)); |
| | | row.setHeight((short)(30 *30)); |
| | | |
| | | // 创建单元格并添加图片 |
| | | Cell cell = row.createCell(0); |
| | |
| | | ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 5), |
| | | cell.getRow().getRowNum() + 1); |
| | | // 计算居中位置 |
| | | int col1 = 1; // 中间列 |
| | | int col2 = col1 + 5; |
| | | int col1 = 3; // 中间列 |
| | | int col2 = col1 + 2; |
| | | anchor.setCol1(col1); |
| | | anchor.setCol2(col2); |
| | | anchor.setRow1(0); |
| | |
| | | row = sheet.createRow(1); |
| | | sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6)); |
| | | Cell titleCell = row.createCell(0); |
| | | titleCell.setCellValue("卷内目录"); |
| | | // 设置加粗和居中样式 |
| | | titleCell.setCellValue("卷 内 目 录"); |
| | | // 设置加粗、宋体、18号字体和居中样式 |
| | | CellStyle style = wb.createCellStyle(); |
| | | Font font = wb.createFont(); |
| | | font.setBold(true); |
| | | font.setFontName("宋体"); |
| | | font.setFontHeightInPoints((short) 18); |
| | | style.setFont(font); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | titleCell.setCellStyle(style); |
| | |
| | | style.setFont(dataFont); |
| | | 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); |
| | | // 表头也设置自动换行 |
| | | style.setWrapText(true); |
| | | styles.put("header", style); |
| | | // 创建表头样式映射,根据Excel注解动态生成 |
| | | 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(), excel.headerFontName(), excel.headerFontBold()); |
| | | if (!headerStyles.containsKey(key)) |
| | | { |
| | | style = wb.createCellStyle(); |
| | | style.cloneStyleFrom(styles.get("data")); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // 根据注解设置表头背景色 - 已注释,表头不显示背景色 |
| | | /*if (excel.headerBackgroundColor() != IndexedColors.WHITE) { |
| | | style.setFillForegroundColor(excel.headerBackgroundColor().index); |
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| | | }*/ |
| | | // 设置表头自动换行 |
| | | style.setWrapText(true); |
| | | Font headerFont = wb.createFont(); |
| | | headerFont.setFontName(excel.headerFontName()); |
| | | headerFont.setFontHeightInPoints((short) 10); |
| | | headerFont.setBold(excel.headerFontBold()); |
| | | headerFont.setColor(excel.headerColor().index); |
| | | style.setFont(headerFont); |
| | | headerStyles.put(key, style); |
| | | } |
| | | } |
| | | styles.putAll(headerStyles); |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | |
| | | // 写入列信息 |
| | | cell.setCellValue(attr.name()); |
| | | setDataValidation(attr, row, column); |
| | | cell.setCellStyle(styles.get("header")); |
| | | // 根据Excel注解动态选择表头样式 |
| | | String key = StringUtils.format("header_{}_{}_{}_{}", attr.headerColor(), attr.headerBackgroundColor(), attr.headerFontName(), attr.headerFontBold()); |
| | | cell.setCellStyle(styles.get(key)); |
| | | return cell; |
| | | } |
| | | |