xlnt C++ Library
repository·master·Indexed 23 days ago
https://github.com/tfussell/xlntA modern, cross-platform C++11+ library for in-memory spreadsheet manipulation and reading/writing XLSX files according to the ECMA 376 4th edition standard. It provides APIs for managing workbooks, worksheets, and cells, including support for cell values, formulas, formatting, named styles, and coordinate-based addressing via the cell_reference module.
What's inside xlnt
- pybind11 is a lightweight, header-only C++ library designed to expose C++ types in Python and vice versa. It is primarily used to create Python bindings for existing C++ code. It aims to minimize boilerplate by using compile-time introspection to infer type information, similar to the syntax and goals of Boost.Python, but optimized for C++11 and newer standards.
Introduction to xlnt
masterxlnt is a modern C++ library designed for in-memory spreadsheet manipulation and reading/writing XLSX files according to the ECMA 376 4th edition standard. It allows developers to programmatically create, modify, and manage spreadsheet data, including cell values, formulas, cell merging, and pane freezing.Explore the xlnt API surface
masterThe xlnt API is organized around core spreadsheet components. The primary entry points for interacting with spreadsheet data are thecellandcell_referencemodules. Usecellto manipulate individual cell values, styles, and properties, andcell_referenceto handle coordinate-based addressing (e.g., 'A1').Supported Workbook Formats and Capabilities
masterxlnt supports reading, editing, and writing various spreadsheet formats and features.
Supported Formats
- Excel-style Workbook: Full Read, Edit, and Write support.
- LibreOffice-style Workbook: Full Read, Edit, and Write support.
- Numbers-style Workbook: Full Read, Edit, and Write support.
- Encrypted Workbooks: Read and Edit support for Excel 2007-2010 and Excel 2013-2016 formats (Write is not supported).
Key Supported Features
- Data & Values: Numeric cell values, Inline string cell values, Shared string cell values, and Shared string text run formatting (e.g., varied fonts within a single cell).
- Formatting: Cell styles, cell formats, alignment, borders, fills, fonts, number formats, and protection (e.g., hiding formulas).
- Metadata & Layout: Document properties, page margins, comments, themes, and XLSX thumbnails.
Unsupported Features
Currently, xlnt does not support Excel Binary (.xlsb), Macro-Enabled (.xlsm, .xltm), Hyperlinks, Formulas (evaluation or values), Charts, Tables, Pivot Tables, or Embedded Content (images).
Core C++ features supported by pybind11
masterpybind11 can map a wide range of C++ features to Python, including:
- Functions: Accepting and returning custom data structures (by value, reference, or pointer), overloaded functions, and instance/static methods.
- Classes & Objects: Instance and static attributes, single and multiple inheritance, and C++ classes with virtual (or pure virtual) methods that can be extended in Python.
- Data Structures: STL data structures, enumerations, iterators, and ranges.
- Memory Management: Smart pointers (like
std::shared_ptr) with reference counting and internal references with correct reference counting. - Other: Arbitrary exception types, callbacks, and custom operators.
Understand the xlnt memory model and value semantics
masterxlnt uses the pimpl idiom (pointer to implementation) for its core data structures. This design choice allows most major objects to be passed and stored by value rather than requiring the use of pointers or references.
When you copy a core object, you are copying a lightweight wrapper that holds an opaque pointer to the actual data stored within the primary
workbookimplementation. Methods called on these wrapper objects dereference the pointer to manipulate the underlying data directly.Because of this model, the following types can be safely passed around and stored by value:
xlnt::workbookxlnt::worksheetxlnt::cellxlnt::formatxlnt::style
#include <iostream> #include <xlnt/xlnt.hpp> void set_cell(xlnt::cell cell, int value) { cell.value(value); } xlnt::workbook create_wb() { xlnt::workbook wb; auto ws = wb.active_sheet(); set_cell(wb.cell("A1"), 2); return wb; } int main() { auto wb = create_wb(); // Note: The example in the source contains a likely typo: wb.value<int>() // In practice, you would access a cell value, e.g., wb.cell("A1").value<int>() std::cout << wb.value<int>() << std::endl; return 0; }Use built-in and custom number formats
masterYou can apply number formats in two ways:
- Built-in formats: Use the static constructors provided by the
xlnt::number_formatclass (e.g.,xlnt::number_format::percentage()). - Custom formats: Pass a specific format string directly to the
xlnt::number_formatconstructor.
Note: The
number_formatdetermines how the value is displayed visually, but does not change the underlying data value.- Built-in formats: Use the static constructors provided by the
Advanced features and 'Goodies' in pybind11
masterBeyond core binding capabilities, pybind11 offers several advanced features:
- Python Compatibility: Supports Python 2.7, 3.x, and PyPy (PyPy2.7 >= 5.7) via an implementation-agnostic interface.
- Performance: Uses C++11 move constructors/assignment for efficient data transfer. It can also use C++14 features (relaxed
constexprand return value deduction) to precompute signatures and reduce binary size. - NumPy Integration: Supports Python's buffer protocols for fast conversion between C++ matrix classes (like Eigen) and NumPy without expensive copies. It can also automatically vectorize functions to apply them to NumPy array arguments.
- Lambda Support: Can bind C++11 lambda functions with captured variables (the capture data is stored in the resulting Python function object).
- Serialization: C++ types can be pickled and unpickled like regular Python objects.
- Ease of Use: Everything is contained in a few header files; no additional libraries need to be linked. Binaries are significantly smaller than Boost.Python equivalents.
Compile xlnt on Ubuntu 16.04 LTS (Xenial Xerus)
masterTo compile xlnt on Ubuntu 16.04, you must ensure you are using at least gcc 6.2.0. Standard APT repositories may only provide older versions (like gcc 5.4.0), which will cause compilation errors such as
workbook.cpp error 1502:31 'extended_property' is not a class, namespace or enumeration.Follow these steps to update the compiler and install xlnt:
- Install dependencies and update compiler:
sudo apt-get update sudo apt-get upgrade sudo apt-get install cmake sudo apt-get install zlibc
Add PPA for newer GCC versions
sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt update sudo apt-get upgrade sudo apt-get install gcc-6 g++-6
Set environment variables to use GCC 6
export CC=/usr/bin/gcc-6
export CXX=/usr/bin/g++-62. **Clone and build xlnt:** ```bash git clone https://github.com/tfussell/xlnt.git xlnt --recurse-submodules cd xlnt cmake . make -j 2 sudo make install- Update shared library links:
sudo ldconfig
- Install dependencies and update compiler:
Compile xlnt from source using CMake
masterxlnt can be built using CMake (v3.2+). The build process involves creating a build directory, configuring the project with CMake, and then running the build tool (e.g.,
make).Basic Build (GNU Make)
mkdir build cd build cmake .. make -j8Build as a Static Library with Xcode
To build a static library instead of a shared library, use the
-D STATIC=ONflag. For Xcode, specify the generator with-G Xcode.mkdir build cd build cmake -D STATIC=ON -G Xcode .. cmake --build .Build for 64-bit Windows (Visual Studio)
On Windows, CMake defaults to 32-bit. To build a 64-bit library using the Visual Studio generator, use the
Win64suffix in the generator name.cmake -G "Visual Studio 14 2015 Win64" ..Note: The resulting shared libraries (e.g.,
libxlnt.dylib) are located in thebuild/libdirectory.Install xlnt using vcpkg
masterYou can install xlnt using the vcpkg dependency manager by following these steps:
- Clone the vcpkg repository.
- Bootstrap vcpkg.
- Integrate vcpkg with your environment.
- Install the xlnt port.
git clone https://github.com/microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ./vcpkg install xlntInstall xlnt on Arch Linux via AUR
masterFor Arch Linux users, xlnt is available in the Arch User Repository (AUR).