我导出了一个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()); }
1 回答
小唯快跑啊
TA贡献1863条经验 获得超2个赞
我认为有必要像在标题单元格中那样在所有单元格中设置样式。
首先,为常规单元格创建样式:
CellStyle regularCellStyle = workbook.createCellStyle();
regularCellStyle setAlignment(HorizontalAlignment.CENTER);
之后,创建单元格并将其存储在变量中。
Cell cellA = row.createCell(0);
cellA.setCellValue(workingTimeReportResponse.getUserName());
cellA.setCellStyle(regularCellStyle);
添加回答
举报
0/150
提交
取消