libxlsxwriter Documentation

repository·main·Indexed 23 days ago

https://github.com/jmcnamara/libxlsxwriter

A lightweight ANSI C library for generating Excel 2007+ XLSX files. It supports cell data, formulas, hyperlinks, complex charts, conditional formatting, and worksheet images. The library features a memory optimization mode for large files and depends only on zlib, with broad support for platforms including Linux, Windows, and macOS.

Tokens
1.4K
Snippets
3
Records
4
Agent score
33%

What's inside libxlsxwriter

  1. Overview of libxlsxwriter

    main

    libxlsxwriter is a C library designed to create Excel 2007+ XLSX files. It allows you to write text, numbers, formulas, and hyperlinks across multiple worksheets.

    Key features include:

    • 100% compatible Excel XLSX files.
    • Full Excel formatting, merged cells, and defined names.
    • Autofilters, charts, and data validation (including dropdown lists).
    • Conditional formatting.
    • Support for worksheet images (PNG, JPEG, GIF) and cell comments.
    • Support for adding Macros.
    • Memory optimization mode for handling large files.

    Technical specifications:

    • Written in ANSI C.
    • Single dependency: zlib.
    • Platform support: Linux, FreeBSD, OpenBSD, OS X, iOS, Windows, MSYS/MSYS2, and Cygwin.
    • Compiler support: GCC, Clang, Xcode, MSVC 2015, ICC, TCC, MinGW, and MingGW-w64/32.
    • Architecture: 32/64 bit, big/little endian.
  2. Create a basic Excel file with libxlsxwriter

    main

    To create an Excel file, you follow a standard workflow:

    1. Create a new workbook using workbook_new().
    2. Add a worksheet using workbook_add_worksheet().
    3. (Optional) Create formats using workbook_add_format() and configure them (e.g., format_set_bold()).
    4. Write data to cells using functions like worksheet_write_string() or worksheet_write_number().
    5. Close the workbook using workbook_close() to finalize the file.

    Note: Always ensure you call workbook_close() to properly save the file to disk.

    #include "xlsxwriter.h"
    
    int main() {
    
        /* Create a new workbook and add a worksheet. */
        lxw_workbook  *workbook  = workbook_new("demo.xlsx");
        lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    
        /* Add a format. */
        lxw_format *format = workbook_add_format(workbook);
    
        /* Set the bold property for the format */
        format_set_bold(format);
    
        /* Change the column width for clarity. */
        worksheet_set_column(worksheet, 0, 0, 20, NULL);
    
        /* Write some simple text. */
        worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
    
        /* Text with formatting. */
        worksheet_write_string(worksheet, 1, 0, "World", format);
    
        /* Write some numbers. */
        worksheet_write_number(worksheet, 2, 0, 123,     NULL);
        worksheet_write_number(worksheet, 3, 0, 123.456, NULL);
    
        /* Insert an image. */
        worksheet_insert_image(worksheet, 1, 2, "logo.png");
    
        workbook_close(workbook);
    
        return 0;
    }
  3. Manage application-level Excel files with lxw_app

    main

    The lxw_app struct represents the top-level application object used for creating Excel XLSX files. It manages the underlying file pointer, document properties, heading pairs (metadata), and part names required for the XLSX package structure.

    Key functions:

    • lxw_app_new(): Allocates and initializes a new lxw_app object.
    • lxw_app_free(lxw_app *app): Releases the memory associated with the application object.
    • lxw_app_add_heading_pair(lxw_app *self, const char *key, const char *value): Adds a metadata heading pair (key-value) to the application.
    • lxw_app_add_part_name(lxw_app *self, const char *name): Registers a part name within the XLSX package.
    • lxw_app_assemble_xml_file(lxw_app *self): Assembles the XML files for the application.
    lxw_app *lxw_app_new(void);
    void lxw_app_free(lxw_app *app);
    void lxw_app_assemble_xml_file(lxw_app *self);
    void lxw_app_add_part_name(lxw_app *self, const char *name);
    void lxw_app_add_heading_pair(lxw_app *self, const char *key, const char *value);
  4. lxw_app Symbol Reference

    main

    The following symbols are exported by include/xlsxwriter/app.h for managing the application-level XLSX structure.

    typedef struct lxw_app {
        FILE *file;
        struct lxw_heading_pairs *heading_pairs;
        struct lxw_part_names *part_names;
        lxw_doc_properties *properties;
        uint32_t num_heading_pairs;
        uint32_t num_part_names;
        uint8_t doc_security;
    } lxw_app;
    
    lxw_app *lxw_app_new(void);
    void lxw_app_free(lxw_app *app);
    void lxw_app_assemble_xml_file(lxw_app *self);
    void lxw_app_add_part_name(lxw_app *self, const char *name);
    void lxw_app_add_heading_pair(lxw_app *self, const char *key, const char *value);