Tuesday, 6 December 2016

How to change font color in Excel Sheet by using Apache POI




By using below code we can change the font color in Apache POI



private static void updateColor(String FilePath,int row,int col,String Sheet, String color1) throws FileNotFoundException, IOException {

Workbook book2=verifyExcelVersion(FilePath);
Sheet sheet2=book2.getSheet(Sheet);

// update Font with colors

CellStyle style=book2.createCellStyle();
Font font=book2.createFont();

if(color1.equals("RED"))
font.setColor(IndexedColors.RED.getIndex());
if(color1.equals("GREEN"))
font.setColor(IndexedColors.GREEN.getIndex());
if(color1.equals("BLUE"))
font.setColor(IndexedColors.BLUE.getIndex());

style.setFont(font);


    sheet2.getRow(row).getCell((short) col).setCellStyle(style);
   
     FileOutputStream output_file =new FileOutputStream(new File(FilePath));
     book2.write(output_file);
     output_file.flush();
     output_file.close();
     sheet2=null;
     book2=null;


}

No comments:

Post a Comment