EasyXLS™ library allows you to import or export rich text format in the cells of an Excel file, by means of HTML syntax when setting the value for the cell.
The following tags are supported: <b> <strong> <i> <u> <s> <strike> <sup> <sub> <font color= face= size=> The above tags have the same meaning as in HTML.
In order to set the underline style for a group of characters EasyXLS uses four specific tags: <underline single> <underline double> <underline single-accounting> <underline double-accounting>
Source code sample
The below example shows how to export rich text format (RTF) in an Excel cell.
// Create an instance of the class that exports Excel files, having two sheets
ExcelDocument workbook = new ExcelDocument(2);
// Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab");
workbook.easy_getSheetAt(1).setSheetName("Second tab");
// Get the table of data for the first worksheet
ExcelTable xlsFirstTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Create the string used to set the RTF in cell
String sRTFValue = "This is <b>bold</b>.";
sRTFValue += "\nThis is <i>italic</i>.";
sRTFValue += "\nThis is <u>underline</u>.";
sRTFValue += "\nThis is <underline double>double underline</underline double>.";
sRTFValue += "\nThis is <font color=red>red</font>.";
sRTFValue += "\nThis is <font color=rgb(255,0,0)>red</font> too.";
sRTFValue += "\nThis is <font face=\"Arial Black\">Arial Black</font>.";
sRTFValue += "\nThis is <font size=15pt>size 15</font>.";
sRTFValue += "\nThis is <s>strikethrough&glt;/s>.";
sRTFValue += "\nThis is <sup>superscript&glt;/sup>.";
sRTFValue += "\nThis is <sub>subscript</sub>.";
sRTFValue += "\n<b>This</b><i>is</i><font color=red face=\"Arial Black\" size=15pt>" +
"<underline double>formatted</underline double></font><s>text</s>.";
// Set the rich text value
xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sRTFValue);
...
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Rich text format.xlsx");
' Create an instance of the class that exports Excel files, having two sheetsDim workbook As New ExcelDocument(2)
' Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab")
workbook.easy_getSheetAt(1).setSheetName("Second tab")
' Get the table of data for the first worksheetDim xlsFirstTab As ExcelWorksheet = workbook.easy_getSheetAt(0)
Dim xlsFirstTable = xlsFirstTab.easy_getExcelTable()
' Create the string used to set the RTF in cellDim sRTFValue As String
sRTFValue = sRTFValue & "This is <b>bold</b>."
sRTFValue = sRTFValue & Chr(10) & "This is <i>italic</i>."
sRTFValue = sRTFValue & Chr(10) & "This is <u>underline</u>."
sRTFValue = sRTFValue & Chr(10) & "This is <underline double>double underline</underline double>."
sRTFValue = sRTFValue & Chr(10) & "This is <font color=red>red</font>."
sRTFValue = sRTFValue & Chr(10) & "This is <font color=rgb(255,0,0)>red</font> too."
sRTFValue = sRTFValue & Chr(10) & "This is <font face=""Arial Black"">Arial Black</font>."
sRTFValue = sRTFValue & Chr(10) & "This is <font size=15pt>size 15</font>."
sRTFValue = sRTFValue & Chr(10) & "This is <s>strikethrough</s>."
sRTFValue = sRTFValue & Chr(10) & "This is <sup>superscript</sup>."
sRTFValue = sRTFValue & Chr(10) & "This is <sub>subscript</sub>."
sRTFValue = sRTFValue & Chr(10) & "<b>This</b> <i>is</i> " & _
"<font color=red face=""Arial Black"" size=15pt>" & _
"<underline double>formatted</underline double></font> <s>text</s>."' Set the rich text value
xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sRTFValue)
...
' Export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Rich text format.xlsx")
C++// Create an instance of the class that exports Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Create two sheets
workbook->easy_addWorksheet_2("First tab");
workbook->easy_addWorksheet_2("Second tab");
// Get the table of data for the first worksheet
EasyXLS::IExcelWorksheetPtr xlsFirstTab =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheetAt(0);
EasyXLS::IExcelTablePtr xlsFirstTable = xlsFirstTab->easy_getExcelTable();
// Create the string used to set the RTF in cellchar* sRTFValue = (char*)malloc(536*sizeof(char));
strcpy(sRTFValue, "This is <b>bold</b>.");
strcat(sRTFValue , "\nThis is <i>italic</i>.");
strcat(sRTFValue , "\nThis is <u>underline</u>.");
strcat(sRTFValue , "\nThis is <underline double>double underline</underline double>.");
strcat(sRTFValue , "\nThis is <font color=red>red</font>.");
strcat(sRTFValue , "\nThis is <font color=rgb(255,0,0)>red</font> too.");
strcat(sRTFValue , "\nThis is <font face=\"Arial Black\">Arial Black</font>.");
strcat(sRTFValue , "\nThis is <font size=15pt>size 15</font>.");
strcat(sRTFValue , "\nThis is <s>strikethrough</s>.");
strcat(sRTFValue , "\nThis is <sup>superscript</sup>.");
strcat(sRTFValue , "\nThis is <sub>subscript</sub>.");
strcat(sRTFValue , "\n<b>This</b> <i>is</i> <font color=red face=\"Arial Black\" size=15pt>");
strcat(sRTFValue , "<underline double>formatted</underline double></font> <s>text</s>.");
// Set the rich text value
xlsFirstTable->easy_getCell(1, 0)->setHTMLValue(sRTFValue);
...
// Export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Rich text format.xlsx");
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that exports Excel files, having two sheets
ExcelDocument ^workbook = gcnew ExcelDocument(2);
// Set the sheet names
workbook->easy_getSheetAt(0)->setSheetName("First tab");
workbook->easy_getSheetAt(1)->setSheetName("Second tab");
// Get the table of data for the first worksheet
ExcelWorksheet ^xlsFirstTab = safe_cast<ExcelWorksheet^>(workbook->easy_getSheetAt(0));
ExcelTable ^xlsFirstTable = xlsFirstTab->easy_getExcelTable();
// Create the string used to set the RTF in cell
String ^sRTFValue = "This is <b>bold</b>.";
sRTFValue = String::Concat(sRTFValue ,"\nThis is <i>italic</i>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <u>underline</u>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <underline double>double underline");
sRTFValue = String::Concat(sRTFValue ,"</underline double>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <font color=red>red</font>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <font color=rgb(255,0,0)>red</font> too.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <font face=\"Arial Black\">Arial Black</font>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <font size=15pt>size 15</font>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <s>strikethrough</s>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <sup>superscript</sup>.");
sRTFValue = String::Concat(sRTFValue ,"\nThis is <sub>subscript</sub>.");
sRTFValue = String::Concat(sRTFValue ,"\n<b>This</b> <i>is</i> ");
sRTFValue = String::Concat(sRTFValue ,"<font color=red face=\"Arial Black\" size=15pt>");
sRTFValue = String::Concat(sRTFValue ,"<underline double>formatted</underline double></font> ");
sRTFValue = String::Concat(sRTFValue ,"<s>text</s>.");
// Set the rich text value
xlsFirstTable->easy_getCell(1, 0)->setHTMLValue(sRTFValue);
...
// Export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Rich text format.xlsx");
Click here to see Continuous Code Listing
// Create an instance of the class that exports Excel files, having two sheets
ExcelDocument workbook = new ExcelDocument(2);
// Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab");
workbook.easy_getSheetAt(1).setSheetName("Second tab");
// Get the table of data for the first worksheet
ExcelTable xlsFirstTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Create the string used to set the RTF in cell
String sRTFValue = "This is <b>bold</b>.";
sRTFValue += "\nThis is <i>italic</i>.";
sRTFValue += "\nThis is <u>underline</u>.";
sRTFValue += "\nThis is <underline double>double underline</underline double>.";
sRTFValue += "\nThis is <font color=red>red</font>.";
sRTFValue += "\nThis is <font color=rgb(255,0,0)>red</font> too.";
sRTFValue += "\nThis is <font face=\"Arial Black\">Arial Black</font>.";
sRTFValue += "\nThis is <font size=15pt>size 15</font>.";
sRTFValue += "\nThis is <s>strikethrough&glt;/s>.";
sRTFValue += "\nThis is <sup>superscript&glt;/sup>.";
sRTFValue += "\nThis is <sub>subscript</sub>.";
sRTFValue += "\n<b>This</b><i>is</i><font color=red face=\"Arial Black\" size=15pt>" +
"<underline double>formatted</underline double></font><s>text</s>.";
// Set the rich text value
xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sRTFValue);
...
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Rich text format.xlsx");
.NET:// Create an instance of the class that exports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Create two sheets
$workbook->easy_addWorksheet_2("First tab");
$workbook->easy_addWorksheet_2("Second tab");
// Get the table of data for the first worksheet
$xlsFirstTable = $workbook->easy_getSheetAt(0)->easy_getExcelTable();
// Create the string used to set the RTF in cell
$sRTFValue = "This is <b>bold</b>.";
$sRTFValue = $sRTFValue ."\nThis is <i>italic</i>.";
$sRTFValue = $sRTFValue ."\nThis is <u>underline</u>.";
$sRTFValue = $sRTFValue ."\nThis is <underline double>double underline</underline double>.";
$sRTFValue = $sRTFValue ."\nThis is <font color=red>red</font>.";
$sRTFValue = $sRTFValue ."\nThis is <font color=rgb(255,0,0)>red</font> too.";
$sRTFValue = $sRTFValue ."\nThis is <font face=\"Arial Black\">Arial Black</font>.";
$sRTFValue = $sRTFValue ."\nThis is <font size=15pt>size 15</font>.";
$sRTFValue = $sRTFValue ."\nThis is <s>strikethrough</s>.";
$sRTFValue = $sRTFValue ."\nThis is <sup>superscript</sup>.";
$sRTFValue = $sRTFValue ."\nThis is <sub>subscript</sub>.";
$sRTFValue = $sRTFValue ."\n<b>This</b> <i>is</i> <font color=red face=\"Arial Black\" size=15pt>";
$sRTFValue = $sRTFValue ."<underline double>formatted</underline double></font> <s>text</s>.";
// Set the rich text value
$xlsFirstTable->easy_getCell(1, 0)->setHTMLValue ($sRTFValue);
...
// Export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Rich text format.xlsx");
Click here to see Continuous Code ListingJava:// Create an instance of the class that exports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Create two sheets
$workbook->easy_addWorksheet("First tab");
$workbook->easy_addWorksheet("Second tab");
// Get the table of data for the first worksheet
$xlsFirstTable = $workbook->easy_getSheetAt(0)->easy_getExcelTable();
// Create the string used to set the RTF in cell
$sFormattedValue = "This is <b>bold</b>.";
$sFormattedValue = $sFormattedValue . "\nThis is <i>italic</i>.";
$sFormattedValue = $sFormattedValue ."\nThis is <u>underline</u>.";
$sFormattedValue = $sFormattedValue ."\nThis is <underline double>double underline</underline double>.";
$sFormattedValue = $sFormattedValue ."\nThis is <font color=red>red</font>.";
$sFormattedValue = $sFormattedValue ."\nThis is <font color=rgb(255,0,0)>red</font> too.";
$sFormattedValue = $sFormattedValue ."\nThis is <font face=\"Arial Black\">Arial Black</font>.";
$sFormattedValue = $sFormattedValue ."\nThis is <font size=15pt>size 15</font>.";
$sFormattedValue = $sFormattedValue ."\nThis is <s>strikethrough</s>.";
$sFormattedValue = $sFormattedValue ."\nThis is <sup>superscript</sup>.";
$sFormattedValue = $sFormattedValue ."\nThis is <sub>subscript</sub>.";
$sFormattedValue = $sFormattedValue ."\n<b>This</b> <i>is</i> <font color=red face=\"Arial Black\" size=15pt>";
$sFormattedValue = $sFormattedValue ."<underline double>formatted</underline double></font> <s>text</s>.";
// Set the rich text value in cell
$xlsFirstTable->easy_getCell(1, 0)->setHTMLValue ($sFormattedValue);
...
// Export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Rich text format.xlsx");
Click here to see Continuous Code Listing
' Create an instance of the class that exports Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Create two sheets
workbook.easy_addWorksheet_2("First tab")
workbook.easy_addWorksheet_2("Second tab")
' Get the table of data for the first worksheetSet xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
' Create the string used to set the RTF in cellDim sRTFValue
sRTFValue = sRTFValue & "This is <b>bold</b>."
sRTFValue = sRTFValue & Chr(10) & "This is <i>italic</i>."
sRTFValue = sRTFValue & Chr(10) & "This is <u>underline</u>."
sRTFValue = sRTFValue & Chr(10) & "This is <underline double>double underline</underline double>."
sRTFValue = sRTFValue & Chr(10) & "This is <font color=red>red</font>."
sRTFValue = sRTFValue & Chr(10) & "This is <font color=rgb(255,0,0)>red</font> too."
sRTFValue = sRTFValue & Chr(10) & "This is <font face=""Arial Black"">Arial Black</font>."
sRTFValue = sRTFValue & Chr(10) & "This is <font size=15pt>size 15</font>."
sRTFValue = sRTFValue & Chr(10) & "This is <s>strikethrough</s>."
sRTFValue = sRTFValue & Chr(10) & "This is <sup>superscript</sup>."
sRTFValue = sRTFValue & Chr(10) & "This is <sub>subscript</sub>."
sRTFValue = sRTFValue & Chr(10) & "<b>This</b> <i>is</i>"
sRTFValue = sRTFValue & "<font color=red face=""Arial Black"" size=15pt>"
sRTFValue = sRTFValue & "<underline double>formatted</underline double></font> <s>text</s>."' Set the rich text value
xlsFirstTable.easy_getCell(1, 0).setHTMLValue (sRTFValue)
...
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Rich text format.xlsx")
<!-- Create an instance of the class that exports Excel files, having two sheets --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Create two sheets --><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 sRTFValue = "This is <b>bold</b>."><cfset sRTFValue = sRTFValue & "\nThis is <i>italic</i>."><cfset sRTFValue = sRTFValue & "\nThis is <u>underline</u>."><cfset sRTFValue = sRTFValue & "\nThis is <underline double>double underline</underline double>."><cfset sRTFValue = sRTFValue & "\nThis is <font color=red>red</font>."><cfset sRTFValue = sRTFValue & "\nThis is <font color=rgb(255,0,0)>red</font> too."><cfset sRTFValue = sRTFValue & "\nThis is <font face=""Arial Black"">Arial Black</font>."><cfset sRTFValue = sRTFValue & "\nThis is <font size=15pt>size 15</font>."><cfset sRTFValue = sRTFValue & "\nThis is <s>strikethrough</s>."><cfset sRTFValue = sRTFValue & "\nThis is <sup>superscript</sup>."><cfset sRTFValue = sRTFValue & "\nThis is <sub>subscript</sub>."><cfset sRTFValue = sRTFValue & "\n<b>This</b> <i>is</i>"><cfset sRTFValue = sRTFValue & "<font color=red face=""Arial Black"" size=15pt><underline double>"><cfset sRTFValue = sRTFValue & "formatted</underline double></font> <s>text</s>."><!-- Set the rich text value --><cfset xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sRTFValue)>
...
<!-- Export Excel file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Rich text format.xlsx")>
.NET:# Create an instance of the class that exports Excel files having two sheets
workbook = ExcelDocument(2)
# Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab")
workbook.easy_getSheetAt(1).setSheetName("Second tab")
# Get the table of data for the first worksheet
xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
# Create the string used to set the RTF in cell
sFormattedValue = "This is <b>bold </b>."
sFormattedValue += "\nThis is <i>italic </i>."
sFormattedValue += "\nThis is <u>underline </u>."
sFormattedValue += "\nThis is <underline double>double underline </underline double>."
sFormattedValue += "\nThis is <font color=red>red </font>."
sFormattedValue += "\nThis is <font color=rgb(255,0,0)>red </font> too."
sFormattedValue += "\nThis is <font face=\"Arial Black\">Arial Black </font>."
sFormattedValue += "\nThis is <font size=15pt>size 15 </font>."
sFormattedValue += "\nThis is <s>strikethrough </s>."
sFormattedValue += "\nThis is <sup>superscript </sup>."
sFormattedValue += "\nThis is <sub>subscript </sub>."
sFormattedValue += "\n <b>This </b> <i>is </i> <font color=red face=\"Arial Black\" ""size=15pt> <underline double>formatted </underline double> </font> <s>text </s>."# Set the rich text value in cell
xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sFormattedValue)
...
# Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Rich text format.xlsx")
Click here to see Continuous Code ListingJava:# Create an instance of the class that exports Excel files having two sheets
workbook = gateway.jvm.ExcelDocument(2)
# Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab")
workbook.easy_getSheetAt(1).setSheetName("Second tab")
# Get the table of data for the first worksheet
xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
# Create the string used to set the RTF in cell
sFormattedValue = "This is <b>bold</b>."
sFormattedValue += "\nThis is <i>italic</i>."
sFormattedValue += "\nThis is <u>underline/<u>."
sFormattedValue += "\nThis is <underline double>double underline</underline double>."
sFormattedValue += "\nThis is <font color=red>red</font>."
sFormattedValue += "\nThis is <font color=rgb(255,0,0)>red</font> too."
sFormattedValue += "\nThis is <font face=\"Arial Black\">Arial Black</font>."
sFormattedValue += "\nThis is <font size=15pt>size 15</font>."
sFormattedValue += "\nThis is <s>strikethrough</s>."
sFormattedValue += "\nThis is <sup>superscript</sup>."
sFormattedValue += "\nThis is <sub>subscript</sub>."
sFormattedValue += "\n<b>This</b> <i>is</i> <font color=red face=\"Arial Black\" ""size=15pt><underline double>formatted</underline double></font> <s>text</s>."# Set the rich text value in cell
xlsFirstTable.easy_getCell(1, 0).setHTMLValue(sFormattedValue)
...
# Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Rich text format.xlsx")
Click here to see Continuous Code Listing
The screen shot below represents the exported Excel file with rich text in cell generated by the code sample above.