1

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
    member variable and intialize it just once – XtremeBaumer Oct 05 '17 at 10:47
  • Please explain and give the code if possible? – Pragati Gupta Oct 05 '17 at 10:52
  • https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html – XtremeBaumer Oct 05 '17 at 10:52
  • @PragatiGupta, your comment is unreadable. Maybe update your original question instead? –  Oct 05 '17 at 11:06
  • Your question is not clear, But what I understand you trying to copy one cell to other cells. Here is a similar question https://stackoverflow.com/questions/33833180/creating-a-new-cell-copies-previous-cells-style-in-apache-poi https://stackoverflow.com/questions/33118987/create-new-or-clone-xssfcellstyle-from-another-xssfcellstyle-poi-apache –  Oct 05 '17 at 11:07
  • @SamDev ,copying one cellstyle to other cells. – Pragati Gupta Oct 05 '17 at 11:23
  • Did you get help from above provided links? This question similar your question –  Oct 05 '17 at 11:41
  • (Please insert a blank after "terminating punctuation marks" (comma, colon, semicolon, full-stop, exclamation and question marks, closing parenthesis, quotes …).) – greybeard Oct 05 '17 at 12:13

1 Answers1

0
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);
}
lukkna
  • 31
  • 3