Excel Italic Cell Font - Java POI Example Program

How to set Excel Cell Font to Italic in POI - Introduction


In this POI formatting options tutorial, we will provide an example code to set a specific excel cell font to italic, with illustrative Java programs. This series covers quite a lot of formatting options with Apache POI, which you can find in the related post section of this blog. 

XLS Format - Set Excel Cell Font to Italic - Java Program Example


A sample Java program to set Excel cell font to Italic is provided below. We use the method setItalic in HSSFFont class, and pass a value of true to this method, to make the font in italic. Let us inspect the output of the program at the end. Here is a program:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class ItalicCellFont {  
        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 font name to Verdana */
                my_font.setFontName("Verdana");
                /* Set font to Italic */
                my_font.setItalic(true);
                /* attach the font to the style created earlier */
                my_style.setFont(my_font);
                
                /* 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 italics, with Verdana font");
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\italic_cell_example.xls"));
                my_workbook.write(out);
                out.close();
        }
}

Italic Cell Font - XLSX File Program


The same method, setItalic can be used for XLSX documents also, but under different classes. Here is an example code to set font to italics, with XLSX format spreadsheets.
import java.io.*;
import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*;
public class ItalicCellFontXLSX {  
        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 font name to Verdana */
                my_font.setFontName("Verdana");
                /* Set font to Italic */
                my_font.setItalic(true);
                
                /* attach the font to the style created earlier */
                my_style.setFont(my_font);
                
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("Italic Cell Font with Verdana Example");
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\italic_Cell_font.xlsx"));
                my_workbook.write(out);
                out.close();
                
        }
}

Output Example


Here is an example output for one of the programs above. You can see that the font is in italics, with the chosen font name.

Italic Cell Font - Excel - Java POI Example - Output
Italic Cell Font - Excel - Java POI Example - Output


No comments:

Post a Comment