Introduction
OpenXLSX provides a powerful fluid API for applying rich text formatting to cells. This allows you to combine multiple text styles—such as different fonts, colors, sizes, and effects—within a single cell.
To use rich text, create an XLRichText object, append runs of text using addRun(), and configure the styling for each run via chainable method calls. Finally, assign the XLRichText object directly to a cell's value.
1. Basic Formatting
You can easily apply basic text styles like bold, italic, strikethrough, and colors. Colors are defined using XLColor with hex codes.
#include <OpenXLSX.hpp>
int main() {
rt1.
addRun(
"Red & Bold: ").setFontColor(
XLColor(
"FF0000")).setBold();
rt1.
addRun(
"Blue & Italic: ").setFontColor(
XLColor(
"0000FF")).setItalic();
rt1.
addRun(
"Strikethrough: ").setStrikethrough(
true);
wks.cell("A1").value() = rt1;
return 0;
}
Definition XLColor.hpp:32
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
void create(std::string_view fileName, bool forceOverwrite=XLForceOverwrite)
Initialize a new .xlsx package from a built-in template.
Definition XLDocument.cpp:372
void close()
Close the document and release underlying ZIP resources, ensuring file handles are closed properly.
Definition XLDocument.cpp:412
void save()
Save changes to the original file path.
Definition XLDocument.cpp:460
XLWorkbook workbook() const
Get the underlying workbook object, containing worksheets and global data state. [[nodiscard]] preven...
Definition XLDocument.cpp:828
A class representing rich text in a cell.
Definition XLRichText.hpp:139
XLRichText & addRun(const XLRichTextRun &run)
Definition XLRichText.hpp:144
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
Definition IXLCellProvider.hpp:24
@ XLUnderlineSingle
Definition XLStyles_Common.hpp:90
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60
2. Fonts and Sizes
You can change the font family and size on a per-run basis.
rt2.
addRun(
"Arial 10, ").setFontName(
"Arial").setFontSize(10);
rt2.
addRun(
"Courier New 14, ").setFontName(
"Courier New").setFontSize(14).setFontColor(
XLColor(
"008000"));
rt2.
addRun(
"Times New Roman 18, ").setFontName(
"Times New Roman").setFontSize(18).setBold();
rt2.
addRun(
"Comic Sans MS 24").setFontName(
"Comic Sans MS").setFontSize(24).setItalic();
wks.cell("A2").value() = rt2;
3. Subscript and Superscript
Subscript and superscript alignments are applied using setVertAlign() with the XLSubscript or XLSuperscript enumerations.
wks.cell("A3").value() = rt3;
@ XLSubscript
Definition XLStyles_Common.hpp:106
@ XLSuperscript
Definition XLStyles_Common.hpp:107
4. Various Underline Types
OpenXLSX supports several underline styles for rich text, including standard and accounting variants.
wks.cell("A4").value() = rt4;
@ XLUnderlineSingleAccounting
Definition XLStyles_Common.hpp:92
@ XLUnderlineDoubleAccounting
Definition XLStyles_Common.hpp:93
@ XLUnderlineDouble
Definition XLStyles_Common.hpp:91
5. Combining Multiple Styles
Because the API is fluid, you can chain as many styling methods as you need onto a single run.
.setFontName("Impact")
.setFontSize(30)
.setBold()
.setItalic()
.setStrikethrough(true)
wks.cell("A5").value() = rt5;