我导出了一个excel文件,如图:
我想为工作表中的文本编辑“align_center”。当我合并两行时,我想为它格式化“中心”。这是它的后端代码:
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
// Row for Header
Row headerRow = sheet.createRow(0);
// Header
for (int col = 0; col < COLUMNTASK.length; col++) {
Cell cell = headerRow.createCell(col);
cell.setCellValue(COLUMNTASK[col]);
cell.setCellStyle(headerCellStyle);
}
for (int col = 0; col < COLUMNTASK.length; col++) {
Cell cell = headerRow.createCell(col);
cell.setCellValue(COLUMNTASK[col]);
cell.setCellStyle(headerCellStyle);
}
int count;
for (int i = 0; i < allTasks.size()-1; i++) {
count = i;
for (int j = count+1; j < allTasks.size(); j++) {
if (allTasks.get(i).getUserName().equals(allTasks.get(j).getUserName())) {
count++;
} else if (count != i) {
sheet.addMergedRegion(new CellRangeAddress(i+1, count+1, 0, 0));
i = count;
} else if (count == i) {
break;
}
if (count == allTasks.size()-1) {
sheet.addMergedRegion(new CellRangeAddress(i+1, count+1, 0, 0));
i = count;
break;
}
}
}
int rowIdx = 1;
for (WorkingTimeReportResponse workingTimeReportResponse : allTasks) {
Row row = sheet.createRow(rowIdx++);
row.createCell(0).setCellValue(workingTimeReportResponse.getUserName());
row.createCell(1).setCellValue(workingTimeReportResponse.getProjectName());
row.createCell(2).setCellValue(workingTimeReportResponse.getIssue());
row.createCell(3).setCellValue(workingTimeReportResponse.getTotalHours());
}
小唯快跑啊
相关分类