What free C++ library for working with Excel on Windows would you recommend?
I need a library that allows:
- Reading data from Excel cells
- Writing data to Excel cells
- Formula support (preferably)
Preferably, the library should have a GPL license.
The best free library for working with Excel in C++ for Windows I would recommend is OpenXLSX. This is an open-source library under the GPL license that provides complete reading, writing, and creation of Excel files (.xlsx), including formula support. OpenXLSX is a cross-platform solution with active development and good documentation.
Table of Contents
- What is OpenXLSX
- Main Library Features
- Installation and Setup
- Usage Examples
- Alternative Options
- Limitations and Features
What is OpenXLSX
OpenXLSX is an open-source C++ library for reading, writing, creating, and modifying Microsoft Excel (.xlsx) files. As stated in the project documentation, this library was developed to address the lack of mature open-source libraries for working with Excel in C++.
The OpenXLSX library supports several important functions, such as creating, opening, and saving spreadsheet files, reading and writing data.
Key features:
- Completely free and open-source
- GPL (General Public License) license
- Cross-platform support (Windows, Linux, macOS)
- Works with the modern .xlsx format
- Does not require Microsoft Excel installation
Main Library Features
Reading and Writing Data Support
OpenXLSX provides full access to Excel cells:
#include <OpenXLSX.hpp>
using namespace OpenXLSX;
int main() {
// Opening an existing Excel file
XLDocument doc;
doc.open("example.xlsx");
auto wks = doc.workbook().worksheet("Sheet1");
// Reading data from cells
auto cellValue = wks.cell("A1").value();
std::cout << "Cell A1 value: " << cellValue << std::endl;
// Writing data to cells
wks.cell("B1").value("Hello, world!");
wks.cell("C1").value(42);
wks.cell("D1").value(3.14);
doc.save();
doc.close();
return 0;
}
Formula Support
The library supports working with Excel formulas:
// Setting a formula in a cell
wks.cell("E1").value("=SUM(A1:D1)");
// Getting the formula value
auto formulaResult = wks.cell("E1").value();
std::cout << "Formula result: " << formulaResult << std::endl;
Additional Features
- Managing workbooks and worksheets
- Reading and writing formatting styles
- Working with cell ranges
- Support for different data types (strings, numbers, dates, boolean values)
- Data export and import
Installation and Setup
System Requirements
- C++17 or higher
- CMake 3.15 or higher
- Modern compiler (MSVC 2019+, GCC 7+, Clang 6+)
Installation Process
- Cloning the repository from GitHub:
git clone https://github.com/troldal/OpenXLSX.git
cd OpenXLSX
- Building the project with CMake:
mkdir build
cd build
cmake ..
cmake --build .
- Integration into Visual Studio project:
- Add the
includedirectory to additional include directories - Reference the built libraries in the linker settings
Example CMakeLists.txt for your project:
cmake_minimum_required(VERSION 3.15)
project(MyExcelApp)
set(CMAKE_CXX_STANDARD 17)
# Adding OpenXLSX
add_subdirectory(third_party/OpenXLSX)
# Creating your application
add_executable(myapp main.cpp)
target_link_libraries(myapp OpenXLSX)
Usage Examples
Creating a New Excel File
#include <OpenXLSX.hpp>
void createExcelFile() {
// Creating a new document
XLDocument doc;
doc.create("new_file.xlsx");
// Getting the workbook and worksheet
auto wks = doc.workbook().worksheet("Sheet1");
// Writing headers
wks.cell("A1").value("Product");
wks.cell("B1").value("Quantity");
wks.cell("C1").value("Price");
wks.cell("D1").value("Amount");
// Writing data
wks.cell("A2").value("Apples");
wks.cell("B2").value(10);
wks.cell("C2").value(50.0);
wks.cell("D2").value("=B2*C2");
wks.cell("A3").value("Oranges");
wks.cell("B3").value(15);
wks.cell("C3").value(75.0);
wks.cell("D3").value("=B3*C3");
// Saving and closing
doc.save();
doc.close();
}
Reading and Processing Data
void readExcelData() {
XLDocument doc;
doc.open("data.xlsx");
auto wks = doc.workbook().worksheet("Sheet1");
// Reading the number of rows
auto lastRow = wks.rowCount();
// Processing data
for (XLRow row : wks.rows()) {
auto productName = row.cell(1).value();
auto quantity = row.cell(2).value();
auto price = row.cell(3).value();
std::cout << "Product: " << productName
<< ", Quantity: " << quantity
<< ", Price: " << price << std::endl;
}
doc.close();
}
Alternative Options
LibXL
LibXL is another popular library for working with Excel:
-
Pros:
- Supports .xls and .xlsx formats
- High performance
- Ease of use
-
Cons:
- Not completely free - has limitations in the free version (up to 300 operations simultaneously)
- License is not GPL, but commercial
- The first row cannot be read or written in the free version
Aspose.Cells for C++
Aspose.Cells is a powerful commercial library:
-
Pros:
- Full support for all Excel features
- Excellent documentation
- Technical support
-
Cons:
- Paid - no free version
- License is not GPL
Limitations and Features
Advantages of OpenXLSX
- Completely free under GPL license
- Open source - can be modified and adapted
- Modern format .xlsx support
- Cross-platform - works on Windows, Linux, macOS
- Active development - regular updates
Current Limitations
- Only .xlsx format support (not .xls)
- Some complex Excel features may have limited support
- Documentation may not be as complete as commercial alternatives
Usage Tips
- Start with simple read/write operations
- Use examples from the GitHub repository
- Keep track of library updates
- If you encounter problems, refer to the issue tracker on GitHub
Conclusion
OpenXLSX is the best choice for free Excel work in C++ on Windows, especially when the GPL license is important. The library provides:
- Full read and write access to Excel cell data
- Formula support for calculations
- Free use without operation quantity limitations
- Compatibility with modern Excel versions
- Active community and development
For most Excel automation tasks in C++, OpenXLSX provides sufficient functionality while remaining completely free and open-source.
Sources
- Open Source C++ Library for Spreadsheets — Reading and Writing Excel Files
- GitHub - OpenXLSX: A C++ library for reading, writing, creating and modifying Microsoft Excel® (.xlsx) files
- C++ library to read Excel Spreadsheets - Software Recommendations Stack Exchange
- C++ Excel Library to read/write xls/xlsx files - LibXL
- Library for working with Excel format
- Creating Excel files in C++ | Creating charts, formulas, tables in Excel files