Working with Excel in C++: Library Overview and Code Examples
Although C++ does not have built-in support for the Excel format, there are several mature libraries that allow you to read, create, and edit .xlsx and .xls files. In this article, we will look at three of the most popular tools: libxlsxwriter, xlsxio, and OpenXLSX. You will learn how to install each of them and get working examples for typical tasks — from report generation to data import.
1. libxlsxwriter — Creating XLSX from Scratch
libxlsxwriter is a lightweight C library for writing Excel 2007+ (XLSX) files. It does not require COM objects or an installed Office. It supports formatting, formulas, charts, cell merging, and conditional formatting.
Installation (Linux / macOS):
git clone https://github.com/jmcnamara/libxlsxwriter.gitcd libxlsxwritermakesudo make installExample: Creating a Simple Report
#include <xlsxwriter.h>
int main() { lxw_workbook *workbook = workbook_new("report.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, "Sales");
// Headers worksheet_write_string(worksheet, 0, 0, "Product", NULL); worksheet_write_string(worksheet, 0, 1, "Quantity", NULL); worksheet_write_string(worksheet, 0, 2, "Price", NULL);
// Data worksheet_write_string(worksheet, 1, 0, "Laptop", NULL); worksheet_write_number(worksheet, 1, 1, 10, NULL); worksheet_write_number(worksheet, 1, 2, 75000, NULL);
// Sum formula worksheet_write_formula(worksheet, 2, 2, "=C2*B2", NULL);
workbook_close(workbook); return 0;}Compilation: g++ -o create_excel create_excel.cpp -lxlsxwriter
Pros: high performance, small size, active support.
Cons: write-only (cannot read existing files).
2. xlsxio — Reading and Writing XLSX
xlsxio is a C library with C++ bindings that supports both reading and writing XLSX files. It is based on minizip and expat. Well suited for streaming processing of large files.
Installation:
git clone https://github.com/brechtsanders/xlsxio.gitcd xlsxiocmake .makesudo make installExample: Reading Data from Excel
#include <xlsxio_read.h>
int main() { xlsxioreader reader = xlsxioread_open("data.xlsx"); if (reader) { xlsxioreadersheet sheet = xlsxioread_sheet_open(reader, NULL, XLSXIOREAD_SKIP_EMPTY_ROWS); while (xlsxioread_sheet_next_row(sheet)) { const char* value; while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) { printf("%s\\t", value); } printf(""); } xlsxioread_sheet_close(sheet); xlsxioread_close(reader); } return 0;}Compilation: g++ -o read_excel read_excel.cpp -lxlsxio_read
Pros: bidirectional operation, support for large files.
Cons: limited formatting (strings and numbers only).
3. OpenXLSX — Modern C++17 Interface
OpenXLSX is a header-only library for C++17 that provides an object-oriented API for reading and writing XLSX. It requires no external dependencies other than the standard library.
Installation:
git clone https://github.com/troldal/OpenXLSX.gitcd OpenXLSXmkdir build && cd buildcmake ..makesudo make installExample: Creating a File with Formatting
#include <OpenXLSX/OpenXLSX.hpp>using namespace OpenXLSX;
int main() {