Working with Excel in C++: Library Overview and Practical Examples
When developing applications in C++, there is often a need to interact with Microsoft Excel files (formats .xls and .xlsx). This can involve exporting reports, importing data, automating spreadsheets, or analyzing large volumes of information. Unlike languages with rich standard libraries (Python, C#), C++ does not have built-in tools for working with Excel, but there are several powerful third-party libraries that solve this problem. In this article, we will look at three popular solutions: xlnt, libxl, and OpenXLSX, and provide code examples for each of them.
1. xlnt Library (Free, Open Source)
xlnt is a modern C++ library for reading, writing, and modifying .xlsx files. It does not depend on COM objects or an installed Excel, making it ideal for cross-platform projects (Windows, Linux, macOS). The library uses the C++14/17 standard and is actively supported by the community.
Key Features:
- Reading and writing cells, rows, columns.
- Style support (fonts, colors, borders, alignment).
- Working with formulas, ranges, merged cells.
- Managing multiple sheets.
- Working with images and charts (limited).
Installation (via vcpkg or building from source):
vcpkg install xlntExample: Creating a simple XLSX file
#include <xlnt/xlnt.hpp>
int main() { xlnt::workbook wb; xlnt::worksheet ws = wb.active_sheet(); ws.title("Report");
// Writing data ws.cell("A1").value("Name"); ws.cell("B1").value("Quantity"); ws.cell("C1").value("Price");
ws.cell("A2").value("Product 1"); ws.cell("B2").value(10); ws.cell("C2").value(250.5);
// Style for headers xlnt::header_footer hf; xlnt::font bold_font(true); ws.cell("A1").font(bold_font);
wb.save("example.xlsx"); return 0;}Example: Reading data from an existing file
#include <xlnt/xlnt.hpp>#include <iostream>
int main() { xlnt::workbook wb; wb.load("example.xlsx"); xlnt::worksheet ws = wb.active_sheet();
for (auto row : ws.rows(false)) { for (auto cell : row) { std::cout << cell.to_string() << "\\t"; } std::cout << std::endl; } return 0;}2. libxl Library (Commercial, Free Version Available)
libxl is a fast and lightweight library for working with .xls and .xlsx. It supports both the old binary Excel 97-2003 format and the modern OOXML. The library is distributed under a commercial license, but a free version with a limitation (watermark on the sheet) is available for non-commercial use.
Key Features:
- Support for both formats: XLS and XLSX.
- Cell formatting: fonts, colors, borders, fill.
- Working with formulas, merged cells, auto-filters.
- Managing sheets, charts (limited).
- High performance when working with large files.
Installation: Download the SDK from the official website (libxl.com) and include the header files and library in your project.
Example: Writing data to XLSX
#include "libxl.h"
int main() { libxl::Book* book = xlCreateXMLBook(); // or xlCreateBook() for XLS if (book) { libxl::Sheet* sheet = book->addSheet("Sheet1"); if (sheet) { sheet Also in library
Prometheus Metrics in Kotlin: Integration with Micrometer and Ktor
Bull for JavaScript (Node.js): Complete Guide with Examples
Generating QR Codes in Node.js: the qrcode Library and Examples
JSON Processing in C# (.NET): Complete Guide to Newtonsoft.Json and System.Text.Json
Java Application Configuration: Libraries and Tools