Introduction
This Quick Start guide demonstrates how to use OpenXLSX to generate a complex, real-world Excel dashboard. It covers almost all major features in the library:
- Worksheet management (renaming, hiding)
- Advanced cell styling (fonts, fills, borders, alignments, number formats)
- Cell merging and Rich Text
- Formulas
- Data validation (cross-sheet dropdown lists)
- Conditional formatting (data bars, color scales)
- Charts (Bar and Line)
- Comments
- Pane freezing and Row/Column sizing
By the end of this tutorial, you will have a comprehensive understanding of how to build sophisticated reports.
1. Document & Worksheet Setup
First, we create a new workbook, rename the default sheet to "Dashboard", and create a hidden "ReferenceData" sheet to hold our dropdown list categories.
#include <OpenXLSX.hpp>
#include <iostream>
int main() {
refWks.cell("A2").value() = "Category B";
refWks.cell("A3").value() = "Category C";
XLCellValueProxy & value()
Retrieves the mutable value proxy for the cell.
Definition XLCell.cpp:253
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
XLWorkbook workbook() const
Get the underlying workbook object, containing worksheets and global data state. [[nodiscard]] preven...
Definition XLDocument.cpp:828
void setName(const std::string &sheetName)
Method for renaming the sheet.
Definition XLSheetBase.hpp:212
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
void addWorksheet(std::string_view sheetName)
Add a new worksheet.
Definition XLWorkbook.cpp:258
XLCellAssignable cell(const std::string &ref) const
Definition XLWorksheet.cpp:168
Definition IXLCellProvider.hpp:24
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60
@ Hidden
Definition XLSheetBase.hpp:35
2. Defining Reusable Styles
OpenXLSX provides a high-level XLStyle facade that allows you to easily configure fonts, fills, borders, number formats, and alignments without worrying about low-level style indices.
Definition XLColor.hpp:32
@ XLAlignCenter
Definition XLStyles_Common.hpp:179
@ XLPatternSolid
Definition XLStyles_Common.hpp:121
@ XLLineStyleDotted
Definition XLStyles_Common.hpp:162
@ XLLineStyleThick
Definition XLStyles_Common.hpp:163
@ XLLineStyleThin
Definition XLStyles_Common.hpp:159
std::optional< XLAlignmentStyle > vertical
Definition XLStyle.hpp:68
std::optional< XLAlignmentStyle > horizontal
Definition XLStyle.hpp:67
std::optional< XLLineStyle > style
Definition XLStyle.hpp:50
BorderElement top
Definition XLStyle.hpp:58
BorderElement bottom
Definition XLStyle.hpp:59
std::optional< XLColor > fgColor
Definition XLStyle.hpp:44
std::optional< XLPatternType > pattern
Definition XLStyle.hpp:43
std::optional< XLColor > color
Definition XLStyle.hpp:34
std::optional< bool > bold
Definition XLStyle.hpp:35
std::optional< std::string > name
Definition XLStyle.hpp:32
std::optional< uint32_t > size
Definition XLStyle.hpp:33
A high-level, human-ergonomic structure representing the styling of a cell or range....
Definition XLStyle.hpp:29
struct OpenXLSX::XLStyle::Font font
struct OpenXLSX::XLStyle::Fill fill
std::optional< std::string > numberFormat
Definition XLStyle.hpp:74
struct OpenXLSX::XLStyle::Border border
struct OpenXLSX::XLStyle::Alignment alignment
3. Merging Cells & Rich Text
Use mergeCells to span content across multiple columns or rows. We also use the XLRichText API to create a multi-styled subtitle.
wks.mergeCells("A1:E2");
wks.cell("A1").value() = "Executive Sales Dashboard 2026";
wks.cell("A1").setStyle(titleStyle);
rtSubtitle.
addRun(
"Confidential: ").setBold().setFontColor(
XLColor(
"FFFF0000"));
rtSubtitle.
addRun(
"Internal Use Only. Data up to ");
wks.mergeCells("A3:E3");
wks.cell("A3").value() = rtSubtitle;
A class representing rich text in a cell.
Definition XLRichText.hpp:139
XLRichText & addRun(const XLRichTextRun &run)
Definition XLRichText.hpp:144
@ XLUnderlineSingle
Definition XLStyles_Common.hpp:90
4. Populating Data & Applying Styles
Now, let's add headers and some mock data, applying the styles we defined earlier using .setStyle().
wks.cell("A4").value() = "Date";
wks.cell("B4").value() = "Product Name";
wks.cell("C4").value() = "Category";
wks.cell("D4").value() = "Units Sold";
wks.cell("E4").value() = "Revenue";
wks.cell("A4").setStyle(headerStyle);
wks.cell("B4").setStyle(headerStyle);
wks.cell("C4").setStyle(headerStyle);
wks.cell("D4").setStyle(headerStyle);
wks.cell("E4").setStyle(headerStyle);
wks.cell("A5").value() = 45383.0;
wks.cell("A5").setStyle(dateStyle);
wks.cell("B5").value() = "Widget X (Enterprise Edition)";
wks.cell("B5").setStyle(dateStyle);
wks.cell("C5").value() = "Category A";
wks.cell("D5").value() = 1500;
wks.cell("E5").value() = 45000.00;
wks.cell("E5").setStyle(currencyStyle);
5. Formulas
You can assign Excel formulas directly to a cell using the .formula() property.
wks.cell("C9").value() = "Total:";
wks.cell("D9").formula() = "SUM(D5:D8)";
wks.cell("E9").formula() = "SUM(E5:E8)";
6. Data Validation & Dropdown Lists
To ensure data integrity, we restrict the "Category" column to only allow values from our hidden ReferenceData sheet.
wks.dataValidations().add("C5:C8")
.requireList("ReferenceData!$A$1:$A$3")
.setPromptMessage("Category Selection", "Choose a category from ReferenceData.");
@ Stop
Definition XLDataValidation.hpp:45
7. Comments
Cell comments are useful for adding notes to specific data points.
wks.comments().addAuthor("System Generated");
wks.comments().set("E9", "These totals are automatically calculated using the SUM formula.", 0);
wks.comments().set("B5", "Top selling product this quarter.", 0);
8. Conditional Formatting
We can use Data Bars to visualize quantities and Color Scales to represent revenue performance directly in the cells.
wks.addConditionalFormatting("D5:D8", dbRule);
wks.addConditionalFormatting("E5:E8", csRule);
OPENXLSX_EXPORT XLCfRule XLDataBarRule(const XLColor &color, bool showValue=true)
Definition XLConditionalFormatting.cpp:168
OPENXLSX_EXPORT XLCfRule XLColorScaleRule(const XLColor &minColor, const XLColor &maxColor)
Definition XLConditionalFormatting.cpp:145
9. Charts
OpenXLSX supports embedding several chart types. We'll add a Bar chart for units and a Line chart for revenue.
auto barChart = wks.addChart(
XLChartType::Bar,
"Units Sold by Product", 11, 1, 400, 250);
barChart.addSeries("Dashboard!$D$5:$D$8", "Units", "Dashboard!$B$5:$B$8");
lineChart.addSeries("Dashboard!$E$5:$E$8", "Revenue", "Dashboard!$A$5:$A$8");
@ Line
Definition XLChart.hpp:37
@ Bar
Definition XLChart.hpp:25
10. Layout: Freezing Panes & Resizing
Finally, adjust the row heights, column widths, and freeze the top 4 rows so headers remain visible when scrolling.
wks.column(1).setWidth(15);
wks.column(2).setWidth(35);
wks.column(5).setWidth(20);
wks.row(1).setHeight(30);
wks.row(4).setHeight(25);
wks.freezePanes("A5");
return 0;
}
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
Compile and run this code, and you will generate a highly professional Excel Dashboard complete with rich functionality!