Change Excel Cell Font Color - Java POI Example Program

Modify Cell Font Color - Introduction


In this series where we discuss different formatting options available with Apache POI when working with Excel spreadsheets, we will discuss how to change a cell font color with an example program. We have already covered the basics of formatting, so we will only provide the program and the output in this post.

Change Cell Font Color Using POI - Java Program Example


We use the "setColor" method in HSSFFont class, to change the font color for a Cell in Java. The complete Java program that changes the color of a cell to red, is given below. This example works for .xls format workbooks.

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class ChangeCellFontColor {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
                /* Get access to HSSFCellStyle */
                HSSFCellStyle my_style = my_workbook.createCellStyle();
                /* Create HSSFFont object from the workbook */
                HSSFFont my_font=my_workbook.createFont();
                /* set the weight of the font */
                my_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                /* Also make the font color to RED */
                my_font.setColor(HSSFFont.COLOR_RED);
                /* attach the font to the style created earlier */
                my_style.setFont(my_font);
                /* At this stage, we have a bold style created which we can attach to a cell */
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("This text will be in bold red color");
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\bold_red_workbook.xls"));
                my_workbook.write(out);
                out.close();
                
                
        }
}

If you want to change cell font color for a .XLSX workbook, you have to use a different class. The example program is given below:

XLSX Format - Change cell font color - Java example


Different set of class needs to be used to handle XLSX formats. Here is an example program that creates a .XLSX workbook, with a cell format set as bold red color.

import java.io.*;
import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*;
public class ChangeCellFontColorXLSX {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet XLSX Format */
                XSSFWorkbook my_workbook = new XSSFWorkbook();
                XSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
                /* Get access to XSSFCellStyle */
                XSSFCellStyle my_style = my_workbook.createCellStyle();
                /* Create XSSFFont object from the workbook */
                XSSFFont my_font=my_workbook.createFont();
                /* set the weight of the font */
                my_font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
                /* Also make the font color to RED */
                my_font.setColor(XSSFFont.COLOR_RED);
                /* attach the font to the style created earlier */
                my_style.setFont(my_font);
                /* At this stage, we have a bold style created which we can attach to a cell */
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("This text will be in bold red color");
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\bold_red_workbook.xlsx"));
                my_workbook.write(out);
                out.close();
                
        }
}

Output Example


In both the programs above, we create different type of workbook formats. The output is consistent. i.e. bold red color in a cell. Here is an example output screenshot:

Change Cell Font Color - Java POI Example - Output
Change Cell Font Color - Java POI Example - Output

No comments:

Post a Comment