17

I am using Apache POI for exporting data into excel sheet. it works fine. the problem is i need apply yellow background color for few rows in the excel sheet while generating the excel sheet. please hellp me how to apply background color for the rows of excel sheet while generating.

Thanks, Reddy

Wivani
  • 2,036
  • 22
  • 28
reddy
  • 701
  • 2
  • 10
  • 10

2 Answers2

41

straight from the official guide:

    // Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
row.setRowStyle(style);
karla
  • 4,506
  • 5
  • 34
  • 39
  • @karla works well. But, If I apply green to some other row, i.e if I apply Yellow to row-2 and green to row-5, row-2 gets green in the end. Any idea – MalTec May 11 '14 at 17:41
  • 1
    However when I create cells in the row without applying this or any other style to these cells, they don't have the row style. It seems I have to add the style to cells as well. – nmy Sep 16 '15 at 11:07
  • 4
    Some correction style.setFillPattern(FillPatternType.BIG_SPOTS); – srsajid Nov 22 '17 at 09:39
1
//Opening excel file
FileInputStream inputStream = new FileInputStream(new File(excelFileLocation));
XSSFWorkbook resultWorkbook = new XSSFWorkbook(inputStream);
XSSFSheet resultSheet = resultWorkbook.getSheet(sheetName);
//Applying style
XSSFRow sheetrow = resultSheet.getRow(1); // Row number     
XSSFCellStyle style = resultWorkbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
sheetrow.getCell(0).setCellStyle(style);//Cell number            

//Saving file       
FileOutputStream outFile =new FileOutputStream(new File(excelFile));
resultWorkbook.write(outFile);
outFile.close();
S Krishna
  • 1,255
  • 13
  • 9