xlnt C++ Library

repository·master·Indexed 23 days ago

https://github.com/tfussell/xlnt

A 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.

Tokens
8.9K
Snippets
8
Records
49
Agent score
75%

What's inside xlnt

  1. What is pybind11?

    master
    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.
  2. Introduction to xlnt

    master
    xlnt 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.
  3. Explore the xlnt API surface

    master
    The xlnt API is organized around core spreadsheet components. The primary entry points for interacting with spreadsheet data are the cell and cell_reference modules. Use cell to manipulate individual cell values, styles, and properties, and cell_reference to handle coordinate-based addressing (e.g., 'A1').
  4. Supported Workbook Formats and Capabilities

    master

    xlnt 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).

  5. Core C++ features supported by pybind11

    master

    pybind11 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.
  6. Understand the xlnt memory model and value semantics

    master

    xlnt 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 workbook implementation. 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::workbook
    • xlnt::worksheet
    • xlnt::cell
    • xlnt::format
    • xlnt::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;    
    }
  7. Use built-in and custom number formats

    master

    You can apply number formats in two ways:

    1. Built-in formats: Use the static constructors provided by the xlnt::number_format class (e.g., xlnt::number_format::percentage()).
    2. Custom formats: Pass a specific format string directly to the xlnt::number_format constructor.

    Note: The number_format determines how the value is displayed visually, but does not change the underlying data value.

  8. Advanced features and 'Goodies' in pybind11

    master

    Beyond 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 constexpr and 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.
  9. Compile xlnt on Ubuntu 16.04 LTS (Xenial Xerus)

    master

    To 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:

    1. 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++-6

    
    2. **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
    1. Update shared library links:
      sudo ldconfig
  10. Compile xlnt from source using CMake

    master

    xlnt 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 -j8

    Build as a Static Library with Xcode

    To build a static library instead of a shared library, use the -D STATIC=ON flag. 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 Win64 suffix in the generator name.

    cmake -G "Visual Studio 14 2015 Win64" ..

    Note: The resulting shared libraries (e.g., libxlnt.dylib) are located in the build/lib directory.

  11. Install xlnt using vcpkg

    master

    You can install xlnt using the vcpkg dependency manager by following these steps:

    1. Clone the vcpkg repository.
    2. Bootstrap vcpkg.
    3. Integrate vcpkg with your environment.
    4. Install the xlnt port.
    git clone https://github.com/microsoft/vcpkg.git
    cd vcpkg
    ./bootstrap-vcpkg.sh
    ./vcpkg integrate install
    ./vcpkg install xlnt