Copying a single HSSFCellStyle to many cells of HSSFWorkbook

259 Views Asked by At

I want to create one HSSFCellSyle and copy it to the cells whose text satisfies some particular conditions.

Currently,I am using a method to create new HSSFCellSyle everytime a cell satisfy conditions,though the styling parameters are the same.

Also,this doesn't work(when limit exceeds) due to limitation on maximum count of HSSFCellStyles for a workbook.

The method goes as:

private static void setCellColor(HSSFWorkbook workbook,HSSFCell cell1){
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle = workbook.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.RED.index);
    cellStyle.setBorderTop((short) 1);
    cell1.setCellStyle(cellStyle);
}

Please suggest how to make only one object of HSSFCellStyle and then just copy it to other cells.

1

There are 1 best solutions below

0
On BEST ANSWER
HSSFCellStyle cellStyle;

private static void setCellColor(HSSFWorkbook workbook, HSSFCell cell1) {
    if (cellStyle == null) {
        cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.RED.index);
        cellStyle.setBorderTop((short) 1);
    }
    cell1.setCellStyle(cellStyle);
}