OpenXLSX

repository·master·Indexed 23 days ago

https://github.com/troldal/openxlsx

A high-performance C++ library for reading, writing, creating, and modifying Microsoft Excel (.xlsx) files. It provides capabilities for manipulating data and formatting, including cell ranges, worksheets, styles, conditional formatting, and merged cells. The library depends on PugiXML and Zippy (or libzip) and requires UTF-8 encoding for all text input and output.

Tokens
7.5K
Snippets
10
Records
45
Agent score
33%

What's inside OpenXLSX

  1. Overview of OpenXLSX

    master
    OpenXLSX is a C++ library designed for reading, writing, creating, and modifying Microsoft Excel® files in the .xlsx format. The library focuses on speed and aims to provide a mature feature set for C++ developers, similar to what is available in Python, Java, or C#.
  2. Current feature support in OpenXLSX

    master

    OpenXLSX is a work in progress. The following features are currently implemented and functional:

    • Create/open/save files
    • Read/write/modify cell contents
    • Copy cells and cell ranges
    • Copy worksheets
    • Cell ranges and iterators
    • Row ranges and iterators
    • Cell content and borders formatting (XLStyles)
    • Conditional formatting
    • Merged cells
    • Worksheet protection (read-only cells, lock with password)
    • Cell comments (plain text)

    Known Limitations:

    • Plots and figures are not implemented and are not planned.
    • Creating const XLDocument objects is currently not working.
  3. Understand OpenXLSX file size limitations

    master

    OpenXLSX uses the miniz library for zip compression/decompression. There is a maximum allowable size for any single entry (e.g., an individual .xml worksheet file) within the .xlsx archive, which is 4 GB (uncompressed).

    Key details:

    • The 4 GB limit applies to individual entries, not the total archive size. You can open workbooks containing multiple large worksheets as long as no single worksheet's XML exceeds 4 GB.
    • For context, a worksheet with 1,048,576 rows and 128 columns filled with 4-digit integers is approximately 4 GB.
    • A 4 GB uncompressed worksheet typically results in a compressed archive size of roughly 300-350 MB.
  4. Work with worksheet comments using XLComments

    master
    OpenXLSX supports creating, reading, modifying, and deleting comments via the XLComments implementation. You can iterate over only the existing comments in a worksheet as they appear in the underlying XML, which is useful when processing files created by other applications.
  5. Handle Unicode and text encoding

    master

    OpenXLSX requires all text input and output to be in UTF-8 encoding. Failure to use UTF-8 may result in unexpected behavior or crashes.

    Requirements

    • Input/Output: All strings passed to or from OpenXLSX must be UTF-8.
    • Source Files: Your C++ source code files must be saved in UTF-8 format. If source files are saved in UTF-16, hard-coded string literals will be treated as UTF-16, which is incompatible with OpenXLSX.
    • Conversions: If you are working with other encodings (like std::wstring on Windows), you must manually convert them to/from UTF-8. OpenXLSX internally uses Boost.Nowide for these conversions.

    Windows Considerations

    Unicode support on Windows terminals can be challenging; non-ASCII characters (e.g., Chinese or Japanese) may appear as gibberish in the terminal. Always ensure your IDE is configured to save files in UTF-8 to avoid crashes when opening files with non-ASCII filenames.

  6. Manage memory usage and performance

    master

    OpenXLSX uses the PugiXML DOM parser, which loads the entire XML document into memory for high-speed manipulation. This can lead to high memory consumption for large spreadsheets.

    Memory Estimates

    Assuming a worksheet with 1,048,576 rows, the following table provides an estimate of column capacity based on available RAM:

    RAMColumns
    8 GB8-16
    16 GB32-64
    32 GB128-256

    Note: Performance depends on data type and your specific environment.

    Optimization: Compact Mode

    If memory is a constraint, you can build OpenXLSX in compact mode by enabling the ENABLE_COMPACT_MODE flag in the CMakeLists.txt file. This enables PugiXML's compact mode, reducing memory usage at the cost of execution speed.

    Recommendation

    It is strongly recommended to use OpenXLSX in 64-bit mode. While 32-bit mode is supported, it is limited to 4 GB of RAM, which significantly restricts the ability to handle large spreadsheets.

  7. Use experimental conditional formatting

    master

    Conditional formatting is currently implemented in an experimental stage.

    Usage Notes & Limitations:

    • Verification Required: Users should verify that generated OOXML files behave correctly in MS Office, as node ordering is not yet guaranteed.
    • Formula Limitation: The current implementation only supports a single <formula> entry per <cfRule>. It does not support cases where <cfRule><formula> appears multiple times.
    • Boolean Storage: Boolean values are stored in XML as "true" and "false". (Note: LibreOffice may use 1 and 0).
    • Differential Formats: Conditional formatting uses differential cell formats (dxfs) via XLStyles. Accessing a format property may automatically create that node if it does not exist.
  8. OpenXLSX Dependencies and Requirements

    master

    OpenXLSX depends on the following 3rd party libraries:

    • PugiXML
    • Zippy (a C++ wrapper around miniz). Alternatively, libzip can be used via the -DOPENXLSX_ENABLE_LIBZIP=ON CMake flag.
    • Boost.Nowide (Required on Windows for handling filenames with non-ASCII/Unicode characters).

    Supported Compilers and Platforms

    PlatformGCCClangMSVC
    WindowsMinGWMinGW+
    MacOS++N/A
    Linux++N/A

    Minimum Compiler Versions:

    • GCC: Version 7
    • Clang: Version 8
    • MSVC: Visual Studio 2019
  9. Integrate OpenXLSX into a CMake project as a subdirectory

    master

    The easiest way to use OpenXLSX is to add its root folder as a subdirectory in your own project's CMakeLists.txt. This ensures the library is built with the same configuration (Debug/Release) as your application, which is critical on Windows when passing STL objects.

    Use add_subdirectory( OpenXLSX ) to make the OpenXLSX::OpenXLSX target available. You can control whether to build a shared library by setting BUILD_SHARED_LIBS to ON.

    # ============================================================================
    # Configure OpenXLSX
    # ============================================================================
    set(OPENXLSX_CREATE_DOCS           OFF)
    set(OPENXLSX_BUILD_SAMPLES         OFF)
    set(         BUILD_SHARED_LIBS     OFF)
    
    add_subdirectory( OpenXLSX )
    
    # Configure linkage for myapp
    target_link_libraries(myapp PRIVATE OpenXLSX::OpenXLSX)
    target_include_directories(myapp PRIVATE ${OpenXLSX_INCLUDES})
  10. Install cmake and git on Debian-based Linux

    master

    To build OpenXLSX, you must have cmake and git installed. On Debian-based Linux distributions, you can install the necessary build tools using apt.

    sudo apt update
    sudo apt install build-essential cmake git
  11. Use a custom Zip library

    master

    By default, OpenXLSX uses Zippy (a wrapper around miniz). If you encounter stability issues with miniz or wish to use a different library (like libzip), you can provide a custom implementation.

    Implementation Steps

    1. Create a wrapper class that conforms to the interface specified by the IZipArchive class.
    2. Use type erasure to implement the interface (no formal inheritance from IZipArchive is required; the class just needs to provide the expected methods).
    3. Pass an instance of your custom class to the OpenXLSX constructor.

    Example

    Refer to Examples/Demo1A for a complete implementation using libzip via a class named CustomZip. To build this specific example, enable the OPENXLSX_ENABLE_LIBZIP_EXAMPLE option in the root CMakeLists.txt and ensure libzip is installed on your system.

  12. Install and configure OpenXLSX on Windows via MSYS2

    master

    For Windows 10/11, it is recommended to use the MSYS2 environment with MSYS Makefiles.

    1. Install MSYS2: Run winget install --id MSYS2.MSYS2 -e in an Administrator PowerShell.
    2. Update MSYS2: In the MSYS2 MSYS shell, run pacman -Syu then pacman -Su.
    3. Install Toolchain: In the MSYS2 MinGW 64-bit shell, run pacman -S --needed base-devel mingw-w64-x86_64-toolchain.
    4. Install CMake & Git: In the MSYS2 MinGW 64-bit shell, run pacman -S --needed mingw-w64-x86_64-cmake mingw-w64-x86_64-git.
    5. Verify: Run git --version; cmake --version; gcc --version; g++ --version in the MinGW 64-bit shell.
    git --version; cmake --version; gcc --version; g++ --version