Apache POI Creating Multiple Fonts Excel Cell using RichTextString

Introduction

In this tutorial we will learn how to create an Excel cell with multiple font styles using Apache POI library.

We will implement different examples to create .xls and xlsx Excel file format using HSSFRichTextString and XSSFRichTextString class in Apache POI.

Apache POI Overview

Apache POI is a open source library to work with Microsoft Office documents in Java.

You can get more information about the project at poi.apache.org

Adding Apache POI Dependencies to Java project

Adding below dependencies to build.gradle file if you are using Gradle build tool.

compile group: 'org.apache.poi', name: 'poi', version: '4.1.1'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.1'

Adding below XML to pom.xml file if you are using Maven build tool.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>

Or you can download the .jar files directly from poi.apache.org/download.html

Example 1 - Using HSSFRichTextString to create cell value

Below Java application to show you how to use HSSFRichTextString object as a cell value to apply multiple fonts one cell.

package dev.simplesolution;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;

public class HSSFRichTextStringExample {
	
	public static void main(String[] args) {
		HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("HSSFRichTextStringExample");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        
        Font fontRed = workbook.createFont();
        fontRed.setBold(true);
        fontRed.setColor(IndexedColors.RED.getIndex());
        
        Font fontGreen = workbook.createFont();
        fontGreen.setItalic(true);
        fontGreen.setUnderline(Font.U_DOUBLE);
        fontGreen.setColor(IndexedColors.GREEN.getIndex());
        
        HSSFRichTextString richTextString = new HSSFRichTextString("Welcome to simplesolution.dev");
        richTextString.applyFont(0, 11, fontRed);
        richTextString.applyFont(11, 29, fontGreen);
        
        cell.setCellValue(richTextString);

        try (OutputStream fileOut = new FileOutputStream("D:\\SimpleSolution\\HSSFRichTextStringExample.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
	}

}
Execute the Java application we will get D:\SimpleSolution\HSSFRichTextStringExample.xls file as below.

HSSFRichTextStringExample.xls

Example 2 - Using XSSFRichTextStringExample to create cell value

Below example showing how to use XSSFRichTextString and create .xlsx Excel file.

package dev.simplesolution;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XSSFRichTextStringExample {
	public static void main(String[] args) {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("XSSFRichTextStringExample");
		XSSFRow row = sheet.createRow(0);
		XSSFCell cell = row.createCell(0);
		
		XSSFRichTextString richTextString = new XSSFRichTextString();
		XSSFFont fontRed = workbook.createFont();
		fontRed.setBold(true);
		fontRed.setColor(IndexedColors.RED.getIndex());
		
		XSSFFont fontGreen = workbook.createFont();
		fontGreen.setItalic(true);
		fontGreen.setUnderline(XSSFFont.U_DOUBLE);
		fontGreen.setColor(IndexedColors.GREEN.getIndex());
		
		richTextString.append("Welcome to ", fontRed);
		richTextString.append("simplesolution.dev", fontGreen);
		
		cell.setCellValue(richTextString);
	
		try (OutputStream fileOut = new FileOutputStream("D:\\SimpleSolution\\XSSFRichTextStringExample.xlsx")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
	}

}

Execute the Java application we will get D:\SimpleSolution\XSSFRichTextStringExample.xlsx file as below.

XSSFRichTextStringExample.xlsx

Conclusion

We have go through 2 Java applications that use Apache POI to create Excel cell with multiple fonts.

Happy Coding 😊

Apache POI Excel Cell Fonts

Apache POI Excel Cell Background Foreground and Font Color with 17 Java Code Examples

Apache POI Excel Cell Border and Border Color

Apache POI Vertical and Horizontal Excel Cell Alignment

Apache POI to Create Excel Date Time Cell in m/d/yy h:mm:ss Format