步骤一:添加相关依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
步骤二:自定义VO类,用于表头合并
@Data
public class ExcelTopVo {
@ApiModelProperty(value="名称")
private String name;
@ApiModelProperty(value="单位")
private String unit = "";
@ApiModelProperty(value="子数据")
private List<ExcelTopVo> child = new ArrayList<>();
}
步骤三:poi实现表头合并
public static XSSFWorkbook genTemplateByTitle(String sheetNames, String title, List<ExcelTopVo> excelTopVos) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet;
if (StringUtils.isBlank(sheetNames)) {
sheet = workbook.createSheet();
}else {
sheet = workbook.createSheet(sheetNames);
}
XSSFCellStyle titleStyle = genStyle(workbook, true, null, true);
XSSFCellStyle normalStyle = genStyle(workbook, false, null, true);
XSSFCellStyle verticalStyle = genStyle(workbook, false, (short) 255, true);
int firstRow = 0;
int firstCol = 0;
if (StringUtils.isNotBlank(title)) {
XSSFRow row = sheet.createRow(firstRow);
row.setHeight((short) (3 * 256));
XSSFCell cell = row.createCell(firstCol);
cell.setCellValue(title);
cell.setCellStyle(titleStyle);
firstRow = 1;
}
List<ExcelMergeVo> excelMergeVos = new ArrayList<>();
XSSFRow firstRowS = sheet.createRow(firstRow);
XSSFRow secondRowS = sheet.createRow(firstRow + 1);
for (ExcelTopVo excelTopVo : excelTopVos) {
ExcelMergeVo excelMergeVo = new ExcelMergeVo();
List<ExcelTopVo> child = excelTopVo.getChild();
XSSFCell cell1 = firstRowS.createCell(firstCol);
String label = excelTopVo.getName();
String unit = excelTopVo.getUnit();
if (StringUtils.isNotBlank(unit)) {
label += unit;
}
if (child.size() > 0) {
excelMergeVo.setFirstRow(firstRow);
excelMergeVo.setLastRow(firstRow);
excelMergeVo.setFirstCol(firstCol);
excelMergeVo.setLaseCol(firstCol + child.size() - 1);
for (ExcelTopVo childExcel : child) {
XSSFCell childCell = secondRowS.createCell(firstCol);
String childLabel = childExcel.getName();
String childUnit = childExcel.getUnit();
if (StringUtils.isNotBlank(childUnit)) {
childLabel += childUnit;
}
childCell.setCellValue(childLabel);
childCell.setCellStyle(normalStyle);
sheet.setColumnWidth(firstCol, sheet.getColumnWidth(firstCol) * 17 / 10);
firstCol++;
}
cell1.setCellValue(label);
cell1.setCellStyle(normalStyle);
sheet.getColumnWidth(firstCol - child.size() +1) * 17 / 10);
} else {
cell1.setCellValue(label);
cell1.setCellStyle(normalStyle);
sheet.autoSizeColumn(firstCol, true);
sheet.setColumnWidth(firstCol, sheet.getColumnWidth(firstCol) * 17 / 10);
XSSFCell cell = secondRowS.createCell(firstCol);
cell.setCellStyle(normalStyle);
excelMergeVo.setFirstRow(firstRow);
excelMergeVo.setLastRow(firstRow + 1);
excelMergeVo.setFirstCol(firstCol);
excelMergeVo.setLaseCol(firstCol);
firstCol++;
}
excelMergeVos.add(excelMergeVo);
}
if (StringUtils.isNotBlank(title)) {
ExcelMergeVo excelMergeVo = new ExcelMergeVo();
excelMergeVo.setFirstCol(0);
excelMergeVo.setLaseCol(firstCol - 1);
excelMergeVo.setFirstRow(0);
excelMergeVo.setLastRow(0);
excelMergeVos.add(excelMergeVo);
}
for (ExcelMergeVo mergeVo : excelMergeVos) {
CellRangeAddress callRangeAddress = new CellRangeAddress(mergeVo.getFirstRow(), mergeVo.getLastRow(), mergeVo.getFirstCol(), mergeVo.getLaseCol());
sheet.addMergedRegion(callRangeAddress);
}
return workbook;
}
private static XSSFCellStyle genStyle(XSSFWorkbook hssfWorkbook, boolean isTitle, Short rotation, boolean isBold) {
XSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
XSSFFont font = hssfWorkbook.createFont();
font.setFontHeightInPoints((short) (isTitle ? 14 : 11));
if (isBold) font.setBold(true);;
cellStyle.setFont(font);
if (null != rotation) cellStyle.setRotation(rotation);
return cellStyle;
}
大功告成,上效果
