我正在工作表中创建多个单元格样式,可以应用于工作表中的不同单元格。
虽然只有一个CellStyle,但代码非常流畅和快速,但在添加多个CellStyles后,它似乎更慢。
是否有更好的方法来处理此问题(在工作表中添加多个单元格样式)
以下是我的工作代码:
public static String createReport(){
try {
workbook = new HSSFWorkbook();
String reportFile = "Report.xls";
System.out.println("reportFile : "+reportFile);
fileOut = new FileOutputStream(reportFile);
worksheet = workbook.createSheet("Report");
cellStyle = worksheet.getWorkbook().createCellStyle();
Font font = worksheet.getWorkbook().createFont();
cellStyle.setFillForegroundColor(HSSFColor.BLACK.index);
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setWrapText(true);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBottomBorderColor(HSSFColor.WHITE.index);
cellStyle.setTopBorderColor(HSSFColor.WHITE.index);
cellStyle.setLeftBorderColor(HSSFColor.WHITE.index);
cellStyle.setRightBorderColor(HSSFColor.WHITE.index);
font.setColor(HSSFColor.WHITE.index);
font.setBold(true);
font.setFontHeightInPoints((short)12);
cellStyle.setFont(font);
//Create a cell style for Pass status
cellStylePass = worksheet.getWorkbook().createCellStyle();
cellStylePass.setFillForegroundColor(HSSFColor.GREEN.index);
cellStylePass.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStylePass.setWrapText(true);
cellStylePass.setAlignment(CellStyle.ALIGN_CENTER);
//cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
cellStylePass.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
cellStylePass.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
cellStylePass.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
cellStylePass.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
cellStylePass.setBottomBorderColor(HSSFColor.WHITE.index);
cellStylePass.setTopBorderColor(HSSFColor.WHITE.index);
cellStylePass.setLeftBorderColor(HSSFColor.WHITE.index);
cellStylePass.setRightBorderColor(HSSFColor.WHITE.index);
cellStylePass.setFont(font);
/*********************FAIL************************/
cellStyleFail = worksheet.getWorkbook().createCellStyle();
cellStyleFail.setFillForegroundColor(HSSFColor.RED.index);
cellStyleFail.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyleFail.setWrapText(true);
cellStyleFail.setAlignment(CellStyle.ALIGN_CENTER);
//cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
cellStyleFail.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
cellStyleFail.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
cellStyleFail.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
cellStyleFail.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
cellStyleFail.setBottomBorderColor(HSSFColor.WHITE.index);
cellStyleFail.setTopBorderColor(HSSFColor.WHITE.index);
cellStyleFail.setLeftBorderColor(HSSFColor.WHITE.index);
cellStyleFail.setRightBorderColor(HSSFColor.WHITE.index);
cellStyleFail.setFont(font);
//Create first Row
Row row1 = worksheet.createRow(0);
CellUtil.createCell(row1, 0, "S.No.",cellStyle);
CellUtil.createCell(row1, 1, "ScenarioName",cellStyle);
CellUtil.createCell(row1, 2, "Environment",cellStyle);
CellUtil.createCell(row1, 3, "STATUS",cellStyle);
CellUtil.createCell(row1, 4, "",cellStyle);
CellUtil.createCell(row1, 5, "",cellStyle);
worksheet.addMergedRegion(new CellRangeAddress(0,0,3,5));
CellUtil.createCell(row1, 6, "Execution Time",cellStyle);
CellUtil.createCell(row1, 7, "",cellStyle);
CellUtil.createCell(row1, 8, "",cellStyle);
worksheet.addMergedRegion(new CellRangeAddress(0,0,6,8));
CellUtil.createCell(row1, 9, "Comments",cellStyle);
//workbook.write(fileOut);
fileOut.flush();
//Create 2nd row
//Write names of bank title
Row row2 = worksheet.createRow(1);
CellUtil.createCell(row2, 3, "TPB", cellStyle);
CellUtil.createCell(row2, 4, "WFB", cellStyle);
CellUtil.createCell(row2, 5, "BOI", cellStyle);
CellUtil.createCell(row2, 6, "TPB", cellStyle);
CellUtil.createCell(row2, 7, "WFB", cellStyle);
CellUtil.createCell(row2, 8, "BOI", cellStyle);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Pass";
}
答案 0 :(得分:0)
我唯一能想到的就是使用你的createReport()方法。如果您只调用该方法一次,那么代码中的所有内容对我来说都很好。如果多次调用该方法,您可能需要将CellStyle定义拉出来,因此它们只创建一次,然后在每次连续的方法调用中重复使用。
旁注:注意创建的CellStyles太多了。创建超过4000将导致java.lang.IllegalStateException
以下更多信息:
https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html#createCellStyle()