How to set rich text format in cells in ColdFusion

EasyXLS Excel library can be used to export Excel files in ColdFusion. The library works without MS Excel installed.

Step 1: Download EasyXLS Excel Library for Java

To download the trial version of EasyXLS Excel Library, press the below button:

Download EasyXLS™ Excel Library for Coldfusion

If you already own a license key, you may login and download EasyXLS from your account.

Install the downloaded EasyXLS installer for v8.6 or earlier.

Step 2: Add EasyXLS library to CLASSPATH in ColdFusion Administator

EasyXLS.jar must be included to class path for Java runtime environment.

EasyXLS.jar can be found:
- Inside the downloaded archive at Step 1 for EasyXLS v9.0 or later
- Under installation path for EasyXLS v8.6 or earlier, in "Lib" folder.

Add EasyXLS library to CLASSPATH

Another solution is to add EasyXLS.jar to "lib" folder from ColdFusion installation path.

Step 3: Run ColdFusion code that sets rich text format in Excel cells

Execute the following ColdFusion code that exports rich text format (RTF) in an Excel cell.

Tutorial 19

This tutorial shows how to create an Excel file in ColdFusion having
multiple sheets. The first sheet is filled with data and the
first cell of the second row contains data in rich text format.

<!-- Constants Classes -->
<cfobject type="java" class="EasyXLS.Constants.DataType" name="DataType" action="CREATE">

Tutorial 19<br>

<!-- Create an instance of the class that exports Excel -->
<cfobject type="java" class="EasyXLS.ExcelDocument" name="workbook" action="CREATE">

<!-- Create two worksheets -->
<cfset ret = workbook.easy_addWorksheet("First tab")>
<cfset ret = workbook.easy_addWorksheet("Second tab")>

<!-- Get the table of data for the first worksheet -->
<cfset xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()>

<!-- Create the string used to set the RTF in cell -->
<cfset sFormattedValue = "This is <b>bold</b>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <i>italic</i>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <u>underline</u>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <underline double>double underline</underline double>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <font color=red>red</font>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <font color=rgb(255,0,0)>red</font> too.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <font face=""Arial Black"">Arial Black</font>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <font size=15pt>size 15</font>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <s>strikethrough</s>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <sup>superscript</sup>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\nThis is <sub>subscript</sub>.">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "\n<b>This</b> <i>is</i> <font color=red face=""Arial Black"" size=15pt>">
<cfset sFormattedValue = sFormattedValue & Chr(10) & "<underline double>formatted</underline double></font> <s>text</s>.">

<!-- Set the rich text value in cell -->
<cfset xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sFormattedValue)>
<cfset xlsFirstTable.easy_getCell(1, 0).setDataType(DataType.STRING)>
<cfset xlsFirstTable.easy_getCell(1, 0).setWrap(true)>
<cfset xlsFirstTable.easy_getRowAt(1).setHeight(250)>
<cfset xlsFirstTable.easy_getColumnAt(0).setWidth(250)>

<!-- Export Excel file -->
Writing file C:\Samples\Tutorial19 - RTF for Excel cells.xlsx<br>
<cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Tutorial19 - RTF for Excel cells.xlsx")>

<!-- Confirm export of Excel file -->
<cfset sError = workbook.easy_getError()>
<cfif (sError is "")>
        File successfully created.
        Error encountered:  #sError#

<!-- Dispose memory -->
<cfset workbook.Dispose()>

EasyXLS Excel libraries:

Excel Library for ColdFusion
full Java version to import, export or convert Excel files
Excel Writer for ColdFusion
Java version to create and export Excel files
Download EasyXLS™ Excel Library for ColdFusion

File formats:

MS Excel 97 - 2003
MS Excel 2007 - 2019
MS Excel 2021
Office 365