reddy
reddy

Reputation: 711

How to apply background color for the rows in excel sheet using Apache POI?

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

Upvotes: 17

Views: 65580

Answers (2)

S Krishna
S Krishna

Reputation: 1333

//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();

Upvotes: 1

karla
karla

Reputation: 4606

straight from the official guide:

    // Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
row.setRowStyle(style);

Upvotes: 40

Related Questions