What free C++ library for working with Excel on Windows would you recommend?
I need an open-source library (preferably with GPL license) for Windows that allows:
- Reading data from Excel cells
- Writing data to Excel cells
- Formula support (if possible)
OpenXLSX is the best free, open-source C++ library for working with Excel on Windows, supporting reading and writing data, as well as working with formulas. This library is actively developed, has good documentation, and is suitable for most Excel file manipulation tasks. Other worthy options include SimpleXlsxWriter and XlsxReaderWriter, which are also open-source solutions with the necessary functionality.
Table of Contents
- Main Recommendations for Choosing a Library
- Detailed Description of OpenXLSX
- Alternative Libraries
- Library Comparison
- Installation Instructions
- Usage Examples
- Conclusion
Main Recommendations for Choosing a Library
For working with Excel files in C++ on Windows, I recommend OpenXLSX as the primary library. This is a completely free, open-source solution that provides all the necessary capabilities:
- Reading data from cells - allows extracting cell values and formatting
- Writing data to cells - supports writing various data types
- Formula support - ability to work with Excel formulas
According to research, OpenXLSX is the most developed and actively maintained library in this category. It uses a modern approach to processing Excel files through ZIP archives, making it reliable and efficient.
Detailed Description of OpenXLSX
OpenXLSX is a C++ library for reading, writing, creating, and modifying Microsoft Excel (.xlsx) files. This library has the following key features:
Main Features
- Support for reading and writing cells with values, formatting, and formulas
- Working with multiple sheets in one file
- Support for cell styles and formatting
- Ability to create and delete rows, columns
- Export data to CSV and other formats
License
OpenXLSX is distributed under the MIT license, which allows free use, modification, and distribution of the code. While this is not GPL, the MIT license is considered very permissive and suitable for most projects.
System Requirements
- Windows 7/8/10/11
- C++11 and above
- CMake support for building the project
- Does not require Microsoft Excel installation
Alternative Libraries
SimpleXlsxWriter
SimpleXlsxWriter is another excellent option for creating XLSX files. According to sources, SimpleXlsxWriter uses the zlib license and is written in C++ using STL functionality.
Advantages:
- Very lightweight and easy to use
- Small dependencies
- Well-suited for creating simple Excel files
Limitations:
- Main function is writing files, reading support is limited
- Fewer features for working with formulas
XlsxReaderWriter
XlsxReaderWriter is an open-source C library for working with XLSX files. As indicated in sources, it allows reading, writing, merging, and editing .xlsx files.
Advantages:
- Support for multiple sheets
- Working with cell formatting
- Open source code
Limitations:
- Written in C, which may be less convenient for C++ projects
- Less documentation compared to OpenXLSX
Library Comparison
| Library | License | Formulas | Reading | Writing | Ease of Use |
|---|---|---|---|---|---|
| OpenXLSX | MIT | Supported | Full | Full | Medium |
| SimpleXlsxWriter | zlib | Limited | Basic | Full | Low |
| XlsxReaderWriter | Open Source | Supported | Full | Full | Medium |
| LibXL | Commercial | Full | Full | Full | Low |
As seen in the table, OpenXLSX offers the most balanced set of features with good formula support and a convenient API. According to research, this library is most often recommended for serious projects.
Installation Instructions
Installing OpenXLSX on Windows
- Clone the repository
git clone https://github.com/troldal/OpenXLSX.git
- Build using CMake
cd OpenXLSX
mkdir build
cd build
cmake ..
cmake --build . --config Release
- Add to Visual Studio project
- Copy header files from the
includedirectory - Add the header file path to your project
- Link the
OpenXLSX.liblibrary
Required Dependencies
- CMake 3.10 or higher
- C++11 compatible compiler
- Standard STL library
Usage Examples
Basic Example of Reading an Excel File with OpenXLSX
#include <OpenXLSX.hpp>
#include <iostream>
int main() {
try {
// Opening an existing Excel file
OpenXLSX::XLDocument doc;
doc.open("example.xlsx");
// Get the first worksheet
auto wks = doc.workbook().worksheet("Sheet1");
// Read value from cell A1
auto cell = wks.cell("A1");
std::cout << "Value in A1: " << cell.value() << std::endl;
// Read value from cell B2 (formula)
auto formulaCell = wks.cell("B2");
std::cout << "Formula in B2: " << formulaCell.value() << std::endl;
doc.close();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
Example of Writing Data to an Excel File
#include <OpenXLSX.hpp>
#include <iostream>
int main() {
try {
// Create a new Excel document
OpenXLSX::XLDocument doc;
doc.create("new_file.xlsx");
// Get the first worksheet
auto wks = doc.workbook().worksheet("Sheet1");
// Write data to cells
wks.cell("A1").value() = "Name";
wks.cell("B1").value() = "Value";
wks.cell("A2").value() = "Product A";
wks.cell("B2").value() = 100;
// Write a formula
wks.cell("B3").value() = "=SUM(B2:B2)";
// Save and close the document
doc.save();
doc.close();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
Working with Multiple Worksheets
// Create a new worksheet
auto newSheet = doc.workbook().addWorksheet("NewSheet");
// Copy data between worksheets
auto sourceSheet = doc.workbook().worksheet("Sheet1");
auto targetSheet = doc.workbook().worksheet("NewSheet");
// Copy cell A1 from one worksheet to another
targetSheet.cell("A1").value() = sourceSheet.cell("A1").value();
Conclusion
For working with Excel files in C++ on Windows, I unequivocally recommend using OpenXLSX. This library provides all the necessary capabilities:
- Full read and write support - you can extract and write data of any types
- Formula support - ability to both read existing formulas and create new ones
- Open source - full access to the code and ability to modify it
- Active development - regular updates and bug fixes
- Good documentation - detailed usage examples
If you need a very simple library for creating Excel files, consider SimpleXlsxWriter. For more complex tasks with an emphasis on reading data, XlsxReaderWriter would be suitable.
All these libraries are free and open-source, making them ideal for use in commercial and non-commercial projects without restrictions.