Excel Rich Text Format Cell Text - Java POI Example Program

Rich Text Format Cell Contents - Introduction


In this tutorial, we will explain how to apply rich text formatting to cell data in Excel, in Java, using Apache POI library. Here is an example of a cell that is rich text formatted:

Rich Text Format Cell Content - HSSFRichTextString - Java POI Example
Rich Text Format Cell Content - HSSFRichTextString - Java POI Example
Here we have multiple styles applied to same cell. Excel allows this kind of styling possible, and Apache POI supports this through a class HSSFRichTextString. We will see how to create such RTF strings in this tutorial.

XLS - RTF - HSSFRichTextString - Java POI Example


The class which helps to get RTF is org.apache.poi.hssf.usermodel.HSSFRichTextString. For the same text provided above, here is a Java program example that explains how to achieve RTF with Apache POI.

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
public class RichTextFormat {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Rich Text Format Example");
                /* We define a rich Text String, that we split into three parts by using applyFont method */
                HSSFFont my_font_1=my_workbook.createFont();
                HSSFFont my_font_2=my_workbook.createFont();
                HSSFFont my_font_3=my_workbook.createFont();
                my_font_1.setColor(Font.COLOR_RED);
                my_font_2.setBoldweight(Font.BOLDWEIGHT_BOLD);
                my_font_3.setItalic(true);
                
                HSSFRichTextString my_rich_text_string = new HSSFRichTextString( "RichTextFormat" );
                my_rich_text_string.applyFont( 0, 4, my_font_1 );
                my_rich_text_string.applyFont( 5, 8, my_font_2);
                my_rich_text_string.applyFont( 8, 13, my_font_3 );
                
                /* Attach these links to cells */
                Row row = my_sheet.createRow(0);                
                Cell cell = row.createCell(0);
                cell.setCellValue(my_rich_text_string);         
                
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\rich_text_format_example.xls"));
                my_workbook.write(out);
                out.close();
        }
}

Here, we attached multiple font objects to HSSFRichTextString class, and achieved a different RTF (first part with a color, second with a bold font and third with a italic font). An example output is shown below:

XLS - Rich Text Format - Java POI Example Program - Output
XLS - Rich Text Format - Java POI Example Program - Output
The same example can be extended to XLSX formats also. We will discuss that in the next part.

XLSX - Rich Text Format - Java POI Example Program


You use org.apache.poi.xssf.usermodel.XSSFRichTextString class to achieve rich text formatting for XLSX documents. Here is an example program that explains how to do this with XLSX files:

import java.io.*;
import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
public class RichTextFormatXLSX {  
        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("RTF Example");
                /* We define a rich Text String, that we split into three parts by using applyFont method */
                XSSFFont my_font_1=my_workbook.createFont();
                XSSFFont my_font_2=my_workbook.createFont();
                XSSFFont my_font_3=my_workbook.createFont();
                my_font_1.setColor(Font.COLOR_RED);
                my_font_2.setBoldweight(Font.BOLDWEIGHT_BOLD);
                my_font_3.setItalic(true);
                
                XSSFRichTextString my_rich_text_string = new XSSFRichTextString( "RichTextFormat" );
                my_rich_text_string.applyFont( 0, 4, my_font_1 );
                my_rich_text_string.applyFont( 5, 8, my_font_2);
                my_rich_text_string.applyFont( 8, 13, my_font_3 );
                
                /* Attach these links to cells */
                Row row = my_sheet.createRow(0);                
                Cell cell = row.createCell(0);
                cell.setCellValue(my_rich_text_string); 
                
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\RTF_Output.xlsx"));
                my_workbook.write(out);
                out.close();
        }
}

The code for XLSX is on the same lines as of XLS format. The above code produces the same output as discussed earlier. That completes a quick introduction to RTF in Excel, using Java and Apache POI. If you have any questions, you can post it in the comments section of this blog.

2 comments:

  1. Can I read back those font colors as in ur example?

    ReplyDelete
  2. Hi, Yes, did you the method getFontAtIndex or getFontOfFormattingRun in the class XSSFRichTextString? Using this you can get an object of type XSSFFont and this has various get methods for you to work this out.

    I can help you with an example if you are stuck. Give a try.

    ReplyDelete