Hssffont Color Example Essay

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.*; publicclass ChangeCellFontColor {publicstaticvoidmain(String[] args)throws Exception{ HSSFWorkbook my_workbook =newHSSFWorkbook(); HSSFSheet my_sheet = my_workbook.createSheet("CellFont"); HSSFCellStyle my_style = my_workbook.createCellStyle(); HSSFFont my_font=my_workbook.createFont(); my_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); my_font.setColor(HSSFFont.COLOR_RED); my_style.setFont(my_font); Row row = my_sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Thistextwillbeinboldredcolor"); cell.setCellStyle(my_style); FileOutputStream out =newFileOutputStream(newFile("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.*; publicclass ChangeCellFontColorXLSX {publicstaticvoidmain(String[] args)throws Exception{ XSSFWorkbook my_workbook =newXSSFWorkbook(); XSSFSheet my_sheet = my_workbook.createSheet("CellFont"); XSSFCellStyle my_style = my_workbook.createCellStyle(); XSSFFont my_font=my_workbook.createFont(); my_font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); my_font.setColor(XSSFFont.COLOR_RED); my_style.setFont(my_font); Row row = my_sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Thistextwillbeinboldredcolor"); cell.setCellStyle(my_style); FileOutputStream out =newFileOutputStream(newFile("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

В свои шестьдесят она была немного тяжеловатой, но все еще весьма привлекательной женщиной, чем не переставала изумлять Бринкерхоффа. Кокетка до мозга костей, трижды разведенная, Мидж двигалась по шестикомнатным директорским апартаментам с вызывающей самоуверенностью. Она отличалась острым умом, хорошей интуицией, частенько засиживалась допоздна и, как говорили, знала о внутренних делах АНБ куда больше самого Господа Бога.

0 thoughts on “Hssffont Color Example Essay

Leave a Reply

Your email address will not be published. Required fields are marked *