Working with Excel in C++: Libraries and Tools for Reading and Writing

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Excel in C++: Libraries and Tools



Working with Excel spreadsheets (format .xlsx) is a common task in application programming. C++ has no built-in support for this format, but there are several high-quality libraries that allow you to read, create, and edit Excel files. In this article, we will look at four main approaches: libxlsxwriter, xlsxio, OpenXLSX, and working through the COM interface on Windows.



1. libxlsxwriter — Creating XLSX "On the Fly"



libxlsxwriter is a C library with C++ wrappers. It allows you to create full-featured XLSX files with support for formatting, formulas, charts, and cell merging. The main advantage is speed and minimal memory consumption.



Installation

Download the source code from GitHub and build it using CMake:

git clone https://github.com/jmcnamara/libxlsxwriter.gitcd libxlsxwritermkdir build && cd buildcmake .. -DCMAKE_BUILD_TYPE=Releasemakesudo make install


Example: Creating a Table with a Formula

#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); worksheet_write_string(worksheet, 0, 3, "Total", NULL);

// Data worksheet_write_string(worksheet, 1, 0, "Apples", NULL); worksheet_write_number(worksheet, 1, 1, 100, NULL); worksheet_write_number(worksheet, 1, 2, 2.5, NULL);

// Formula worksheet_write_formula(worksheet, 1, 3, "=B2*C2", NULL);

workbook_close(workbook); return 0;}


To compile, use:

g++ -o create_excel create_excel.cpp -lxlsxwriter


2. xlsxio — Reading and Simple Writing



xlsxio is a lightweight C library focused on reading XLSX files. It supports streaming parsing, allowing you to process large files without loading them into memory.



Installation

git clone https://github.com/brechtsanders/xlsxio.gitcd xlsxiomkdir build && cd buildcmake ..makesudo make install


Example: Reading Data from Excel

#include <xlsxio_read.h>

int main() { xlsxioreader xlsxioread = xlsxioread_open("data.xlsx"); if (!xlsxioread) return 1;

xlsxioreadersheet sheet = xlsxioread_sheet_open(xlsxioread, NULL, 0); if (sheet) { 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(xlsxioread); return 0;}


3. OpenXLSX — Modern C++ Interface



OpenXLSX is a C++17 library that provides an object-oriented API for working with XLSX. It supports reading, writing, styles, and working with multiple sheets.



Installation

git clone https://github.com/troldal/OpenXLSX.gitcd OpenXLSXmkdir build && cd buildcmake .. -DOPENXLSX_BUILD_TESTS=OFFmakesudo make install


Example: Writing with Formatting

#include <OpenXLSX/OpenXLSX.hpp>

int main() { using namespace OpenXLSX;

XLDocument doc; doc.create("formatted.xlsx"); auto wks = doc.workbook().worksheet("Sheet1");

// Writing with format XLCellValue v

Recommendations