Reputation: 287
I am using Apache POI to create an excel file. I am using short hand notation to create cells and I want to know if there is a way to fill the colors on the cells using the same coding pattern.
Coding standard 1.
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue(NYPG3Constants.FIRST_NAME);
headerRow.createCell(1).setCellValue(NYPG3Constants.LAST_NAME);
headerRow.createCell(2).setCellValue(NYPG3Constants.POLICY_NUMBER);
headerRow.createCell(3).setCellValue(NYPG3Constants.ZIP_CODE);
headerRow.createCell(4).setCellValue(NYPG3Constants.DATE_OF_BIRTH);
I can set styles using following coding pattern but for every cell headers I need to create a separate cell object.
Coding standard 2
CellStyle style = workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.GOLD.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
Cell cell = headerRow.createCell(0);
cell.setCellValue(NYPG3Constants.FIRST_NAME);
cell.setCellStyle(style);
Is there a way to fill colors on the header cells using my first coding standard?
Thanks in advance
Upvotes: 0
Views: 3796
Reputation: 61890
What about a simple for
loop?
CellStyle style = workBook.createCellStyle();
style.setFillForegroundColor(HSSFColor.GOLD.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
...
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue(NYPG3Constants.FIRST_NAME);
headerRow.createCell(1).setCellValue(NYPG3Constants.LAST_NAME);
headerRow.createCell(2).setCellValue(NYPG3Constants.POLICY_NUMBER);
headerRow.createCell(3).setCellValue(NYPG3Constants.ZIP_CODE);
headerRow.createCell(4).setCellValue(NYPG3Constants.DATE_OF_BIRTH);
for (int c = 0; c < 5; c++) {
headerRow.getCell(c).setCellStyle(style);
}
Upvotes: 2