File: output_buffer.c

package info (click to toggle)
libxlsxwriter 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,864 kB
  • sloc: ansic: 65,601; python: 2,106; makefile: 693; perl: 229; sh: 224; xml: 168; cpp: 73; javascript: 5
file content (42 lines) | stat: -rw-r--r-- 1,281 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Example of using libxlsxwriter to write a workbook file to a memory buffer.
 *
 * Copyright 2014-2025, John McNamara, jmcnamara@cpan.org
 *
 */

#include <stdio.h>

#include "xlsxwriter.h"

int main() {
    const char *output_buffer;
    size_t output_buffer_size;

    /* Set the worksheet options. */
    lxw_workbook_options options = {.output_buffer = &output_buffer,
                                    .output_buffer_size = &output_buffer_size,
                                    .constant_memory = LXW_FALSE,
                                    .tmpdir = NULL,
                                    .use_zip64 = LXW_FALSE};

    /* Create a new workbook with options. */
    lxw_workbook  *workbook  = workbook_new_opt(NULL, &options);
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
    worksheet_write_number(worksheet, 1, 0, 123,     NULL);

    lxw_error error = workbook_close(workbook);

    if (error)
        return error;

    /* Do something with the XLSX data in the output buffer. */
    FILE *file = fopen("output_buffer.xlsx", "wb");
    fwrite(output_buffer, output_buffer_size, 1, file);
    fclose(file);
    free((void *)output_buffer);

    return ferror(stdout);
}