libxlsxwriter
|
The Workbook is the main object exposed by the libxlsxwriter library. It represents the entire spreadsheet as you see it in Excel and internally it represents the Excel file as it is written on disk.
Functions | |
lxw_workbook * | workbook_new (const char *filename) |
Create a new workbook object. | |
lxw_workbook * | workbook_new_opt (const char *filename, lxw_workbook_options *options) |
Create a new workbook object, and set the workbook options. | |
lxw_worksheet * | workbook_add_worksheet (lxw_workbook *workbook, const char *sheetname) |
Add a new worksheet to a workbook. | |
lxw_chartsheet * | workbook_add_chartsheet (lxw_workbook *workbook, const char *sheetname) |
Add a new chartsheet to a workbook. | |
lxw_format * | workbook_add_format (lxw_workbook *workbook) |
Create a new Format object to formats cells in worksheets. | |
lxw_chart * | workbook_add_chart (lxw_workbook *workbook, uint8_t chart_type) |
Create a new chart to be added to a worksheet: | |
lxw_error | workbook_close (lxw_workbook *workbook) |
Close the Workbook object and write the XLSX file. | |
lxw_error | workbook_set_properties (lxw_workbook *workbook, lxw_doc_properties *properties) |
Set the document properties such as Title, Author etc. | |
lxw_error | workbook_set_custom_property_string (lxw_workbook *workbook, const char *name, const char *value) |
Set a custom document text property. | |
lxw_error | workbook_set_custom_property_number (lxw_workbook *workbook, const char *name, double value) |
Set a custom document number property. | |
lxw_error | workbook_set_custom_property_boolean (lxw_workbook *workbook, const char *name, uint8_t value) |
Set a custom document boolean property. | |
lxw_error | workbook_set_custom_property_datetime (lxw_workbook *workbook, const char *name, lxw_datetime *datetime) |
Set a custom document date or time property. | |
lxw_error | workbook_define_name (lxw_workbook *workbook, const char *name, const char *formula) |
Create a defined name in the workbook to use as a variable. | |
lxw_format * | workbook_get_default_url_format (lxw_workbook *workbook) |
Get the default URL format used with worksheet_write_url() . | |
lxw_worksheet * | workbook_get_worksheet_by_name (lxw_workbook *workbook, const char *name) |
Get a worksheet object from its name. | |
lxw_chartsheet * | workbook_get_chartsheet_by_name (lxw_workbook *workbook, const char *name) |
Get a chartsheet object from its name. | |
lxw_error | workbook_validate_sheet_name (lxw_workbook *workbook, const char *sheetname) |
Validate a worksheet or chartsheet name. | |
lxw_error | workbook_add_vba_project (lxw_workbook *workbook, const char *filename) |
Add a vbaProject binary to the Excel workbook. | |
lxw_error | workbook_add_signed_vba_project (lxw_workbook *workbook, const char *vba_project, const char *signature) |
Add a vbaProject binary and a vbaProjectSignature binary to the Excel workbook. | |
lxw_error | workbook_set_vba_name (lxw_workbook *workbook, const char *name) |
Set the VBA name for the workbook. | |
void | workbook_read_only_recommended (lxw_workbook *workbook) |
Add a recommendation to open the file in "read-only" mode. | |
lxw_workbook * workbook_new | ( | const char * | filename | ) |
filename | The name of the new Excel file to create. |
The workbook_new()
constructor is used to create a new Excel workbook with a given filename:
When specifying a filename it is recommended that you use an .xlsx
extension or Excel will generate a warning when opening the file.
lxw_workbook * workbook_new_opt | ( | const char * | filename, |
lxw_workbook_options * | options | ||
) |
filename | The name of the new Excel file to create. |
options | Workbook options. |
This function is the same as the workbook_new()
constructor but allows additional options to be set.
The options that can be set via lxw_workbook_options are:
constant_memory
: This option reduces the amount of data stored in memory so that large files can be written efficiently. This option is off by default. See the note below for limitations when this mode is on.tmpdir
: libxlsxwriter stores workbook data in temporary files prior to assembling the final XLSX file. The temporary files are created in the system's temp directory. If the default temporary directory isn't accessible to your application, or doesn't contain enough space, you can specify an alternative location using the tmpdir
option.use_zip64
: Make the zip library use ZIP64 extensions when writing very large xlsx files to allow the zip container, or individual XML files within it, to be greater than 4 GB. See ZIP64 on Wikipedia for more information. This option is off by default.output_buffer
: Output to a memory buffer instead of a file. The buffer must be freed manually by calling free()
. This option can only be used if filename is NULL.output_buffer_size
: Used with output_buffer to get the size of the created buffer. This option can only be used if filename is NULL
.constant_memory
mode each row of in-memory data is written to disk and then freed when a new row is started via one of the worksheet_write_*()
functions. Therefore, once this option is active data should be written in sequential row by row order. For this reason worksheet_merge_range()
and some other row based functionality doesn't work in this mode. See Constant Memory Mode for more details.constant_memory
mode the library uses temp file storage for worksheet data. This can lead to an issue on OSes that map the /tmp
directory into memory since it is possible to consume the "system" memory even though the "process" memory remains constant. In these cases you should use an alternative temp file location by using the tmpdir
option shown above. See Constant memory mode and the /tmp directory for more details. lxw_worksheet * workbook_add_worksheet | ( | lxw_workbook * | workbook, |
const char * | sheetname | ||
) |
workbook | Pointer to a lxw_workbook instance. |
sheetname | Optional worksheet name, defaults to Sheet1, etc. |
The workbook_add_worksheet()
function adds a new worksheet to a workbook.
At least one worksheet should be added to a new workbook: The Worksheet object is used to write data and configure a worksheet in the workbook.
The sheetname
parameter is optional. If it is NULL
the default Excel convention will be followed, i.e. Sheet1, Sheet2, etc.:
The worksheet name must be a valid Excel worksheet name, i.e:
[ ] : * ? / \
If any of these errors are encountered the function will return NULL. You can check for valid name using the workbook_validate_sheet_name()
function.
lxw_chartsheet * workbook_add_chartsheet | ( | lxw_workbook * | workbook, |
const char * | sheetname | ||
) |
workbook | Pointer to a lxw_workbook instance. |
sheetname | Optional chartsheet name, defaults to Chart1, etc. |
The workbook_add_chartsheet()
function adds a new chartsheet to a workbook. The Chartsheet object is like a worksheet except it displays a chart instead of cell data.
The sheetname
parameter is optional. If it is NULL
the default Excel convention will be followed, i.e. Chart1, Chart2, etc.:
The chartsheet name must be a valid Excel worksheet name, i.e.:
[ ] : * ? / \
If any of these errors are encountered the function will return NULL. You can check for valid name using the workbook_validate_sheet_name()
function.
At least one worksheet should be added to a new workbook when creating a chartsheet in order to provide data for the chart. The Worksheet object is used to write data and configure a worksheet in the workbook.
lxw_format * workbook_add_format | ( | lxw_workbook * | workbook | ) |
workbook | Pointer to a lxw_workbook instance. |
The workbook_add_format()
function can be used to create new Format objects which are used to apply formatting to a cell.
See the Format object and Working with Formats sections for more details about Format properties and how to set them.
lxw_chart * workbook_add_chart | ( | lxw_workbook * | workbook, |
uint8_t | chart_type | ||
) |
workbook | Pointer to a lxw_workbook instance. |
chart_type | The type of chart to be created. See lxw_chart_type. |
The workbook_add_chart()
function creates a new chart object that can be added to a worksheet:
The available chart types are defined in lxw_chart_type. The types of charts that are supported are:
Chart type | Description |
---|---|
LXW_CHART_AREA | Area chart. |
LXW_CHART_AREA_STACKED | Area chart - stacked. |
LXW_CHART_AREA_STACKED_PERCENT | Area chart - percentage stacked. |
LXW_CHART_BAR | Bar chart. |
LXW_CHART_BAR_STACKED | Bar chart - stacked. |
LXW_CHART_BAR_STACKED_PERCENT | Bar chart - percentage stacked. |
LXW_CHART_COLUMN | Column chart. |
LXW_CHART_COLUMN_STACKED | Column chart - stacked. |
LXW_CHART_COLUMN_STACKED_PERCENT | Column chart - percentage stacked. |
LXW_CHART_DOUGHNUT | Doughnut chart. |
LXW_CHART_LINE | Line chart. |
LXW_CHART_LINE_STACKED | Line chart - stacked. |
LXW_CHART_LINE_STACKED_PERCENT | Line chart - percentage stacked. |
LXW_CHART_PIE | Pie chart. |
LXW_CHART_SCATTER | Scatter chart. |
LXW_CHART_SCATTER_STRAIGHT | Scatter chart - straight. |
LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS | Scatter chart - straight with markers. |
LXW_CHART_SCATTER_SMOOTH | Scatter chart - smooth. |
LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS | Scatter chart - smooth with markers. |
LXW_CHART_RADAR | Radar chart. |
LXW_CHART_RADAR_WITH_MARKERS | Radar chart - with markers. |
LXW_CHART_RADAR_FILLED | Radar chart - filled. |
See chart.h for details.
lxw_error workbook_close | ( | lxw_workbook * | workbook | ) |
workbook | Pointer to a lxw_workbook instance. |
The workbook_close()
function closes a Workbook object, writes the Excel file to disk, frees any memory allocated internally to the Workbook and frees the object itself.
The workbook_close()
function returns any lxw_error error codes encountered when creating the Excel file. The error code can be returned from the program main or the calling function:
lxw_error workbook_set_properties | ( | lxw_workbook * | workbook, |
lxw_doc_properties * | properties | ||
) |
workbook | Pointer to a lxw_workbook instance. |
properties | Document properties to set. |
The workbook_set_properties
function can be used to set the document properties of the Excel file created by libxlsxwriter
. These properties are visible when you use the Office Button -> Prepare -> Properties
option in Excel and are also available to external applications that read or index windows files.
The properties that can be set are:
title
subject
author
manager
company
category
keywords
comments
hyperlink_base
created
The properties are specified via a lxw_doc_properties
struct. All the fields are all optional. An example of how to create and pass the properties is:
The created
parameter sets the file creation date/time shown in Excel. This defaults to the current time and date if set to 0. If you wish to create files that are binary equivalent (for the same input data) then you should set this creation date/time to a known value using a time_t
value.
lxw_error workbook_set_custom_property_string | ( | lxw_workbook * | workbook, |
const char * | name, | ||
const char * | value | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | The name of the custom property. |
value | The value of the custom property. |
The workbook_set_custom_property_string()
function can be used to set one or more custom document text properties not covered by the standard properties in the workbook_set_properties()
function above.
For example:
There are 4 workbook_set_custom_property_string_*()
functions for each of the custom property types supported by Excel:
workbook_set_custom_property_string()
workbook_set_custom_property_number()
workbook_set_custom_property_datetime()
workbook_set_custom_property_boolean()
Note: the name and value parameters are limited to 255 characters by Excel.
lxw_error workbook_set_custom_property_number | ( | lxw_workbook * | workbook, |
const char * | name, | ||
double | value | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | The name of the custom property. |
value | The value of the custom property. |
Set a custom document number property. See workbook_set_custom_property_string()
above for details.
lxw_error workbook_set_custom_property_boolean | ( | lxw_workbook * | workbook, |
const char * | name, | ||
uint8_t | value | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | The name of the custom property. |
value | The value of the custom property. |
Set a custom document boolean property. See workbook_set_custom_property_string()
above for details.
lxw_error workbook_set_custom_property_datetime | ( | lxw_workbook * | workbook, |
const char * | name, | ||
lxw_datetime * | datetime | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | The name of the custom property. |
datetime | The value of the custom property. |
Set a custom date or time number property. See workbook_set_custom_property_string()
above for details.
lxw_error workbook_define_name | ( | lxw_workbook * | workbook, |
const char * | name, | ||
const char * | formula | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | The defined name. |
formula | The cell or range that the defined name refers to. |
This function is used to defined a name that can be used to represent a value, a single cell or a range of cells in a workbook: These defined names can then be used in formulas:
As in Excel a name defined like this is "global" to the workbook and can be referred to from any worksheet:
It is also possible to define a local/worksheet name by prefixing it with the sheet name using the syntax ‘'sheetname!definedname’`:
If the sheet name contains spaces or special characters you must follow the Excel convention and enclose it in single quotes:
The rules for names in Excel are explained in the Microsoft Office documentation.
lxw_format * workbook_get_default_url_format | ( | lxw_workbook * | workbook | ) |
workbook | Pointer to a lxw_workbook instance. |
This function returns a lxw_format instance that is used for the default blue underline hyperlink in the worksheet_write_url()
function when a format isn't specified:
The format is the hyperlink style defined by Excel for the default theme. This format is only ever required when overwriting a string URL with data of a different type. See the example below.
lxw_worksheet * workbook_get_worksheet_by_name | ( | lxw_workbook * | workbook, |
const char * | name | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | Worksheet name. |
This function returns a lxw_worksheet object reference based on its name:
lxw_chartsheet * workbook_get_chartsheet_by_name | ( | lxw_workbook * | workbook, |
const char * | name | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | chartsheet name. |
This function returns a lxw_chartsheet object reference based on its name:
lxw_error workbook_validate_sheet_name | ( | lxw_workbook * | workbook, |
const char * | sheetname | ||
) |
workbook | Pointer to a lxw_workbook instance. |
sheetname | Sheet name to validate. |
This function is used to validate a worksheet or chartsheet name according to the rules used by Excel:
[ ] : * ? / \
This function is called by workbook_add_worksheet()
and workbook_add_chartsheet()
but it can be explicitly called by the user beforehand to ensure that the sheet name is valid.
lxw_error workbook_add_vba_project | ( | lxw_workbook * | workbook, |
const char * | filename | ||
) |
workbook | Pointer to a lxw_workbook instance. |
filename | The path/filename of the vbaProject.bin file. |
The workbook_add_vba_project()
function can be used to add macros or functions to a workbook using a binary VBA project file that has been extracted from an existing Excel xlsm file:
Only one vbaProject.bin
file can be added per workbook. The name doesn't have to be vbaProject.bin
. Any suitable path/name for an existing VBA bin file will do.
Once you add a VBA project had been add to an libxlsxwriter workbook you should ensure that the file extension is .xlsm
to prevent Excel from giving a warning when it opens the file:
See also Working with VBA Macros
lxw_error workbook_add_signed_vba_project | ( | lxw_workbook * | workbook, |
const char * | vba_project, | ||
const char * | signature | ||
) |
workbook | Pointer to a lxw_workbook instance. |
vba_project | The path/filename of the vbaProject.bin file. |
signature | The path/filename of the vbaProjectSignature.bin file. |
The workbook_add_signed_vba_project()
function can be used to add digitally signed macros or functions to a workbook. The function adds a binary VBA project file and a binary VBA project signature file that have been extracted from an existing Excel xlsm file with digitally signed macros:
Only one vbaProject.bin
file can be added per workbook. The name doesn't have to be vbaProject.bin
. Any suitable path/name for an existing VBA bin file will do. The same applies for vbaProjectSignature.bin
.
See also Working with VBA Macros
lxw_error workbook_set_vba_name | ( | lxw_workbook * | workbook, |
const char * | name | ||
) |
workbook | Pointer to a lxw_workbook instance. |
name | Name of the workbook used by VBA. |
The workbook_set_vba_name()
function can be used to set the VBA name for the workbook. This is sometimes required when a vbaProject macro included via workbook_add_vba_project()
refers to the workbook by a name other than ThisWorkbook
.
If an Excel VBA name for the workbook isn't specified then libxlsxwriter will use ThisWorkbook
.
See also Working with VBA Macros
void workbook_read_only_recommended | ( | lxw_workbook * | workbook | ) |
workbook | Pointer to a lxw_workbook instance. |
This function can be used to set the Excel "Read-only Recommended" option that is available when saving a file. This presents the user of the file with an option to open it in "read-only" mode. This means that any changes to the file can't be saved back to the same file and must be saved to a new file. It can be set as follows:
Which will raise a dialog like the following when opening the file:
typedef struct lxw_doc_properties lxw_doc_properties |
Workbook document properties. Set any unused fields to NULL or 0.
typedef struct lxw_workbook_options lxw_workbook_options |
Optional parameters when creating a new Workbook object via workbook_new_opt().
The following properties are supported:
constant_memory
: This option reduces the amount of data stored in memory so that large files can be written efficiently. This option is off by default. See the notes below for limitations when this mode is on.tmpdir
: libxlsxwriter stores workbook data in temporary files prior to assembling the final XLSX file. The temporary files are created in the system's temp directory. If the default temporary directory isn't accessible to your application, or doesn't contain enough space, you can specify an alternative location using the tmpdir
option.use_zip64
: Make the zip library use ZIP64 extensions when writing very large xlsx files to allow the zip container, or individual XML files within it, to be greater than 4 GB. See ZIP64 on Wikipedia for more information. This option is off by default.output_buffer
: Output to a buffer instead of a file. The buffer must be freed manually by calling free(). This option can only be used if filename is NULL.output_buffer_size
: Used with output_buffer to get the size of the created buffer. This option can only be used if filename is NULL.constant_memory
mode each row of in-memory data is written to disk and then freed when a new row is started via one of the worksheet_write_*()
functions. Therefore, once this option is active data should be written in sequential row by row order. For this reason worksheet_merge_range()
and some other row based functionality doesn't work in this mode. See Constant Memory Mode for more details.constant_memory
mode the library uses temp file storage for worksheet data. This can lead to an issue on OSes that map the /tmp
directory into memory since it is possible to consume the "system" memory even though the "process" memory remains constant. In these cases you should use an alternative temp file location by using the tmpdir
option shown above. See Constant memory mode and the /tmp directory for more details. typedef struct lxw_workbook lxw_workbook |
The members of the lxw_workbook struct aren't modified directly. Instead the workbook properties are set by calling the functions shown in workbook.h.
#define LXW_FOREACH_WORKSHEET | ( | worksheet, | |
workbook | |||
) | STAILQ_FOREACH((worksheet), (workbook)->worksheets, list_pointers) |
This macro allows you to loop over all the worksheets that have been added to a workbook. You must provide a lxw_worksheet pointer and a pointer to the lxw_workbook:
Data Structures | |
struct | lxw_doc_properties |
struct | lxw_workbook_options |
Workbook options. More... | |
struct | lxw_workbook |
Struct to represent an Excel workbook. More... | |
Typedefs | |
typedef struct lxw_doc_properties | lxw_doc_properties |
typedef struct lxw_workbook_options | lxw_workbook_options |
Workbook options. | |
typedef struct lxw_workbook | lxw_workbook |
Struct to represent an Excel workbook. | |
Macros | |
#define | LXW_FOREACH_WORKSHEET(worksheet, workbook) STAILQ_FOREACH((worksheet), (workbook)->worksheets, list_pointers) |
Macro to loop over all the worksheets in a workbook. | |