libxlsxwriter
|
The Worksheet object represents an Excel worksheet. It handles operations such as writing data to cells or formatting worksheet layout.
A Worksheet object isn't created directly. Instead a worksheet is created by calling the workbook_add_worksheet() function from a Workbook object:
Functions | |
lxw_error | worksheet_write_number (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, double number, lxw_format *format) |
Write a number to a worksheet cell. | |
lxw_error | worksheet_write_string (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *string, lxw_format *format) |
Write a string to a worksheet cell. | |
lxw_error | worksheet_write_formula (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format) |
Write a formula to a worksheet cell. | |
lxw_error | worksheet_write_array_formula (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, const char *formula, lxw_format *format) |
Write an array formula to a worksheet cell. | |
lxw_error | worksheet_write_dynamic_array_formula (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, const char *formula, lxw_format *format) |
Write an Excel 365 dynamic array formula to a worksheet range. | |
lxw_error | worksheet_write_dynamic_formula (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format) |
Write an Excel 365 dynamic array formula to a worksheet cell. | |
lxw_error | worksheet_write_datetime (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_datetime *datetime, lxw_format *format) |
Write a date or time to a worksheet cell. | |
lxw_error | worksheet_write_unixtime (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, int64_t unixtime, lxw_format *format) |
Write a Unix datetime to a worksheet cell. | |
lxw_error | worksheet_write_url (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *url, lxw_format *format) |
lxw_error | worksheet_write_boolean (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, int value, lxw_format *format) |
Write a formatted boolean worksheet cell. | |
lxw_error | worksheet_write_blank (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_format *format) |
Write a formatted blank worksheet cell. | |
lxw_error | worksheet_write_formula_num (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format, double result) |
Write a formula to a worksheet cell with a user defined numeric result. | |
lxw_error | worksheet_write_formula_str (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format, const char *result) |
Write a formula to a worksheet cell with a user defined string result. | |
lxw_error | worksheet_write_rich_string (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_rich_string_tuple *rich_string[], lxw_format *format) |
Write a "Rich" multi-format string to a worksheet cell. | |
lxw_error | worksheet_write_comment (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *string) |
Write a comment to a worksheet cell. | |
lxw_error | worksheet_write_comment_opt (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *string, lxw_comment_options *options) |
Write a comment to a worksheet cell with options. | |
lxw_error | worksheet_set_row (lxw_worksheet *worksheet, lxw_row_t row, double height, lxw_format *format) |
Set the properties for a row of cells. | |
lxw_error | worksheet_set_row_opt (lxw_worksheet *worksheet, lxw_row_t row, double height, lxw_format *format, lxw_row_col_options *options) |
Set the properties for a row of cells. | |
lxw_error | worksheet_set_row_pixels (lxw_worksheet *worksheet, lxw_row_t row, uint32_t pixels, lxw_format *format) |
Set the properties for a row of cells, with the height in pixels. | |
lxw_error | worksheet_set_row_pixels_opt (lxw_worksheet *worksheet, lxw_row_t row, uint32_t pixels, lxw_format *format, lxw_row_col_options *options) |
Set the properties for a row of cells, with the height in pixels. | |
lxw_error | worksheet_set_column (lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, double width, lxw_format *format) |
Set the properties for one or more columns of cells. | |
lxw_error | worksheet_set_column_opt (lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, double width, lxw_format *format, lxw_row_col_options *options) |
Set the properties for one or more columns of cells with options. | |
lxw_error | worksheet_set_column_pixels (lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, uint32_t pixels, lxw_format *format) |
Set the properties for one or more columns of cells, with the width in pixels. | |
lxw_error | worksheet_set_column_pixels_opt (lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, uint32_t pixels, lxw_format *format, lxw_row_col_options *options) |
Set the properties for one or more columns of cells with options, with the width in pixels. | |
lxw_error | worksheet_insert_image (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *filename) |
Insert an image in a worksheet cell. | |
lxw_error | worksheet_insert_image_opt (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *filename, lxw_image_options *options) |
Insert an image in a worksheet cell, with options. | |
lxw_error | worksheet_insert_image_buffer (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const unsigned char *image_buffer, size_t image_size) |
Insert an image in a worksheet cell, from a memory buffer. | |
lxw_error | worksheet_insert_image_buffer_opt (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const unsigned char *image_buffer, size_t image_size, lxw_image_options *options) |
Insert an image in a worksheet cell, from a memory buffer. | |
lxw_error | worksheet_embed_image (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *filename) |
Embed an image in a worksheet cell. | |
lxw_error | worksheet_embed_image_opt (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *filename, lxw_image_options *options) |
Embed an image in a worksheet cell, with options. | |
lxw_error | worksheet_embed_image_buffer (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const unsigned char *image_buffer, size_t image_size) |
Embed an image in a worksheet cell, from a memory buffer. | |
lxw_error | worksheet_embed_image_buffer_opt (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const unsigned char *image_buffer, size_t image_size, lxw_image_options *options) |
Embed an image in a worksheet cell, from a memory buffer. | |
lxw_error | worksheet_set_background (lxw_worksheet *worksheet, const char *filename) |
Set the background image for a worksheet. | |
lxw_error | worksheet_set_background_buffer (lxw_worksheet *worksheet, const unsigned char *image_buffer, size_t image_size) |
Set the background image for a worksheet, from a buffer. | |
lxw_error | worksheet_insert_chart (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_chart *chart) |
Insert a chart object into a worksheet. | |
lxw_error | worksheet_insert_chart_opt (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_chart *chart, lxw_chart_options *user_options) |
Insert a chart object into a worksheet, with options. | |
lxw_error | worksheet_merge_range (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, const char *string, lxw_format *format) |
Merge a range of cells. | |
lxw_error | worksheet_autofilter (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col) |
Set the autofilter area in the worksheet. | |
lxw_error | worksheet_filter_column (lxw_worksheet *worksheet, lxw_col_t col, lxw_filter_rule *rule) |
Write a filter rule to an autofilter column. | |
lxw_error | worksheet_filter_column2 (lxw_worksheet *worksheet, lxw_col_t col, lxw_filter_rule *rule1, lxw_filter_rule *rule2, uint8_t and_or) |
Write two filter rules to an autofilter column. | |
lxw_error | worksheet_filter_list (lxw_worksheet *worksheet, lxw_col_t col, const char **list) |
Write multiple string filters to an autofilter column. | |
lxw_error | worksheet_data_validation_cell (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_data_validation *validation) |
Add a data validation to a cell. | |
lxw_error | worksheet_data_validation_range (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, lxw_data_validation *validation) |
Add a data validation to a range. | |
lxw_error | worksheet_conditional_format_cell (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_conditional_format *conditional_format) |
Add a conditional format to a worksheet cell. | |
lxw_error | worksheet_conditional_format_range (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, lxw_conditional_format *conditional_format) |
Add a conditional format to a worksheet range. | |
lxw_error | worksheet_insert_button (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_button_options *options) |
Insert a button object into a worksheet. | |
lxw_error | worksheet_add_table (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, lxw_table_options *options) |
Add an Excel table to a worksheet. | |
void | worksheet_activate (lxw_worksheet *worksheet) |
Make a worksheet the active, i.e., visible worksheet. | |
void | worksheet_select (lxw_worksheet *worksheet) |
Set a worksheet tab as selected. | |
void | worksheet_hide (lxw_worksheet *worksheet) |
Hide the current worksheet. | |
void | worksheet_set_first_sheet (lxw_worksheet *worksheet) |
Set current worksheet as the first visible sheet tab. | |
void | worksheet_freeze_panes (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col) |
Split and freeze a worksheet into panes. | |
void | worksheet_split_panes (lxw_worksheet *worksheet, double vertical, double horizontal) |
Split a worksheet into panes. | |
lxw_error | worksheet_set_selection (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col) |
Set the selected cell or cells in a worksheet: | |
void | worksheet_set_top_left_cell (lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col) |
Set the first visible cell at the top left of a worksheet. | |
void | worksheet_set_landscape (lxw_worksheet *worksheet) |
Set the page orientation as landscape. | |
void | worksheet_set_portrait (lxw_worksheet *worksheet) |
Set the page orientation as portrait. | |
void | worksheet_set_page_view (lxw_worksheet *worksheet) |
Set the page layout to page view mode. | |
void | worksheet_set_paper (lxw_worksheet *worksheet, uint8_t paper_type) |
Set the paper type for printing. | |
void | worksheet_set_margins (lxw_worksheet *worksheet, double left, double right, double top, double bottom) |
Set the worksheet margins for the printed page. | |
lxw_error | worksheet_set_header (lxw_worksheet *worksheet, const char *string) |
Set the printed page header caption. | |
lxw_error | worksheet_set_footer (lxw_worksheet *worksheet, const char *string) |
Set the printed page footer caption. | |
lxw_error | worksheet_set_header_opt (lxw_worksheet *worksheet, const char *string, lxw_header_footer_options *options) |
Set the printed page header caption with additional options. | |
lxw_error | worksheet_set_footer_opt (lxw_worksheet *worksheet, const char *string, lxw_header_footer_options *options) |
Set the printed page footer caption with additional options. | |
lxw_error | worksheet_set_h_pagebreaks (lxw_worksheet *worksheet, lxw_row_t breaks[]) |
Set the horizontal page breaks on a worksheet. | |
lxw_error | worksheet_set_v_pagebreaks (lxw_worksheet *worksheet, lxw_col_t breaks[]) |
Set the vertical page breaks on a worksheet. | |
void | worksheet_print_across (lxw_worksheet *worksheet) |
Set the order in which pages are printed. | |
void | worksheet_set_zoom (lxw_worksheet *worksheet, uint16_t scale) |
Set the worksheet zoom factor. | |
void | worksheet_gridlines (lxw_worksheet *worksheet, uint8_t option) |
Set the option to display or hide gridlines on the screen and the printed page. | |
void | worksheet_center_horizontally (lxw_worksheet *worksheet) |
Center the printed page horizontally. | |
void | worksheet_center_vertically (lxw_worksheet *worksheet) |
Center the printed page vertically. | |
void | worksheet_print_row_col_headers (lxw_worksheet *worksheet) |
Set the option to print the row and column headers on the printed page. | |
lxw_error | worksheet_repeat_rows (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_row_t last_row) |
Set the number of rows to repeat at the top of each printed page. | |
lxw_error | worksheet_repeat_columns (lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col) |
Set the number of columns to repeat at the top of each printed page. | |
lxw_error | worksheet_print_area (lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col) |
Set the print area for a worksheet. | |
void | worksheet_fit_to_pages (lxw_worksheet *worksheet, uint16_t width, uint16_t height) |
Fit the printed area to a specific number of pages both vertically and horizontally. | |
void | worksheet_set_start_page (lxw_worksheet *worksheet, uint16_t start_page) |
Set the start/first page number when printing. | |
void | worksheet_set_print_scale (lxw_worksheet *worksheet, uint16_t scale) |
Set the scale factor for the printed page. | |
void | worksheet_print_black_and_white (lxw_worksheet *worksheet) |
Set the worksheet to print in black and white. | |
void | worksheet_right_to_left (lxw_worksheet *worksheet) |
Display the worksheet cells from right to left for some versions of Excel. | |
void | worksheet_hide_zero (lxw_worksheet *worksheet) |
Hide zero values in worksheet cells. | |
void | worksheet_set_tab_color (lxw_worksheet *worksheet, lxw_color_t color) |
Set the color of the worksheet tab. | |
void | worksheet_protect (lxw_worksheet *worksheet, const char *password, lxw_protection *options) |
Protect elements of a worksheet from modification. | |
void | worksheet_outline_settings (lxw_worksheet *worksheet, uint8_t visible, uint8_t symbols_below, uint8_t symbols_right, uint8_t auto_style) |
Set the Outline and Grouping display properties. | |
void | worksheet_set_default_row (lxw_worksheet *worksheet, double height, uint8_t hide_unused_rows) |
Set the default row properties. | |
lxw_error | worksheet_set_vba_name (lxw_worksheet *worksheet, const char *name) |
Set the VBA name for the worksheet. | |
void | worksheet_show_comments (lxw_worksheet *worksheet) |
Make all comments in the worksheet visible. | |
void | worksheet_set_comments_author (lxw_worksheet *worksheet, const char *author) |
Set the default author of the cell comments. | |
lxw_error | worksheet_ignore_errors (lxw_worksheet *worksheet, uint8_t type, const char *range) |
Ignore various Excel errors/warnings in a worksheet for user defined ranges. | |
lxw_error worksheet_write_number | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
double | number, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
number | The number to write to the cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_number()
function writes numeric types to the cell specified by row
and column
:
The native data type for all numbers in Excel is a IEEE-754 64-bit double-precision floating point, which is also the default type used by worksheet_write_number
.
The format
parameter is used to apply formatting to the cell. This parameter can be NULL
to indicate no formatting or it can be a Format object.
NaN
, Inf
or -Inf
as a number value. If you are writing data that contains these values then your application should convert them to a string or handle them in some other way. lxw_error worksheet_write_string | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | string, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
string | String to write to cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_string()
function writes a string to the cell specified by row
and column
:
The format
parameter is used to apply formatting to the cell. This parameter can be NULL
to indicate no formatting or it can be a Format object:
Unicode strings are supported in UTF-8 encoding. This generally requires that your source file is UTF-8 encoded or that the data has been read from a UTF-8 source:
lxw_error worksheet_write_formula | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | formula, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
formula | Formula string to write to cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_formula()
function writes a formula or function to the cell specified by row
and column
:
The format
parameter is used to apply formatting to the cell. This parameter can be NULL
to indicate no formatting or it can be a Format object.
Libxlsxwriter doesn't calculate the value of a formula and instead stores a default value of 0
. The correct formula result is displayed in Excel, as shown in the example above, since it recalculates the formulas when it loads the file. For cases where this is an issue see the worksheet_write_formula_num()
function and the discussion in that section.
Formulas must be written with the US style separator/range operator which is a comma (not semi-colon). Therefore a formula with multiple values should be written as follows:
See also Working with Formulas.
lxw_error worksheet_write_array_formula | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col, | ||
const char * | formula, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
formula | Array formula to write to cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_array_formula()
function writes an array formula to a cell range. In Excel an array formula is a formula that performs a calculation on a set of values.
In Excel an array formula is indicated by a pair of braces around the formula: {=SUM(A1:B1*A2:B2)}
.
Array formulas can return a single value or a range or values. For array formulas that return a range of values you must specify the range that the return values will be written to. This is why this function has first_
and last_
row/column parameters. The RANGE() macro can also be used to specify the range:
If the array formula returns a single value then the first_
and last_
parameters should be the same:
lxw_error worksheet_write_dynamic_array_formula | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col, | ||
const char * | formula, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
formula | Dynamic Array formula to write to cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_dynamic_array_formula()
function writes an Excel 365 dynamic array formula to a cell range. Some examples of functions that return dynamic arrays are:
FILTER
RANDARRAY
SEQUENCE
SORTBY
SORT
UNIQUE
XLOOKUP
XMATCH
Dynamic array formulas and their usage in libxlsxwriter is explained in detail Dynamic Array support. The following is a example usage:
This formula gives the results shown in the image below.
The need for the _xlfn._xlws.
prefix in the formula is explained in Formulas added in Excel 2010 and later.
lxw_error worksheet_write_dynamic_formula | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | formula, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
formula | Formula string to write to cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_dynamic_formula()
function is similar to the worksheet_write_dynamic_array_formula()
function, shown above, except that it writes a dynamic array formula to a single cell, rather than a range. This is a syntactic shortcut since the array range isn't generally known for a dynamic range and specifying the initial cell is sufficient for Excel, as shown in the example below:
This formula gives the following result:
The need for the _xlfn.
and _xlfn._xlws.
prefixes in the formula is explained in Formulas added in Excel 2010 and later.
lxw_error worksheet_write_datetime | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_datetime * | datetime, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
datetime | The datetime to write to the cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_datetime()
function can be used to write a date or time to the cell specified by row
and column
:
The format
parameter should be used to apply formatting to the cell using a Format object as shown above. Without a date format the datetime will appear as a number only.
See Working with Dates and Times for more information about handling dates and times in libxlsxwriter.
lxw_error worksheet_write_unixtime | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
int64_t | unixtime, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
unixtime | The Unix datetime to write to the cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_unixtime()
function can be used to write dates and times in Unix date format to the cell specified by row
and column
. Unix Time which is a common integer time format. It is defined as the number of seconds since the Unix epoch (1970-01-01 00:00 UTC). Negative values can also be used for dates prior to 1970:
The format
parameter should be used to apply formatting to the cell using a Format object as shown above. Without a date format the datetime will appear as a number only.
The output from this code sample is:
Unixtime is generally represented with a 32 bit time_t
type which has a range of approximately 1900-12-14 to 2038-01-19. To access the full Excel date range of 1900-01-01 to 9999-12-31 this function uses a 64 bit parameter.
See Working with Dates and Times for more information about handling dates and times in libxlsxwriter.
lxw_error worksheet_write_url | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | url, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
url | The url to write to the cell. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_url()
function is used to write a URL/hyperlink to a worksheet cell specified by row
and column
.
The format
parameter is used to apply formatting to the cell. This parameter can be NULL
, in which case the default Excel blue underlined hyperlink style will be used. If required a user defined Format object can be used: underline:
The usual web style URI's are supported: http://
, https://
, ftp://
and mailto:
:
An Excel hyperlink is comprised of two elements: the displayed string and the non-displayed link. By default the displayed string is the same as the link. However, it is possible to overwrite it with any other libxlsxwriter
type using the appropriate worksheet_write_*()
function. The most common case is to overwrite the displayed link text with another string. To do this we must also match the default URL format using workbook_get_default_url_format()
:
Two local URIs are supported: internal:
and external:
. These are used for hyperlinks to internal worksheet references or external workbook and worksheet references:
Worksheet references are typically of the form Sheet1!A1
. You can also link to a worksheet range using the standard Excel notation: Sheet1!A1:B2
.
In external links the workbook and worksheet name must be separated by the #
character:
You can also link to a named range in the target worksheet: For example say you have a named range called my_name
in the workbook c:\temp\foo.xlsx
you could link to it as follows:
Excel requires that worksheet names containing spaces or non alphanumeric characters are single quoted as follows:
Links to network files are also supported. Network files normally begin with two back slashes as follows \\NETWORK\etc
. In order to represent this in a C string literal the backslashes should be escaped:
Alternatively, you can use Unix style forward slashes. These are translated internally to backslashes:
Note:
libxlsxwriter will escape the following characters in URLs as required by Excel: \s " < > \ [ ] ^ { }
. Existing URL %xx
style escapes in the string are ignored to allow for user-escaped strings.
Note:
The maximum allowable URL length in recent versions of Excel is 2079 characters. In older versions of Excel (and libxlsxwriter <= 0.8.8) the limit was 255 characters.
lxw_error worksheet_write_boolean | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
int | value, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
value | The boolean value to write to the cell. |
format | A pointer to a Format instance or NULL. |
Write an Excel boolean to the cell specified by row
and column
:
lxw_error worksheet_write_blank | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
format | A pointer to a Format instance or NULL. |
Write a blank cell specified by row
and column
:
This function is used to add formatting to a cell which doesn't contain a string or number value.
Excel differentiates between an "Empty" cell and a "Blank" cell. An Empty cell is a cell which doesn't contain data or formatting whilst a Blank cell doesn't contain data but does contain formatting. Excel stores Blank cells but ignores Empty cells.
As such, if you write an empty cell without formatting it is ignored.
lxw_error worksheet_write_formula_num | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | formula, | ||
lxw_format * | format, | ||
double | result | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
formula | Formula string to write to cell. |
format | A pointer to a Format instance or NULL. |
result | A user defined numeric result for the formula. |
The worksheet_write_formula_num()
function writes a formula or Excel function to the cell specified by row
and column
with a user defined numeric result:
Libxlsxwriter doesn't calculate the value of a formula and instead stores the value 0
as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.
This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications.
However, applications that don't have a facility to calculate formulas, such as Excel Viewer, or some mobile applications will only display the 0
results.
If required, the worksheet_write_formula_num()
function can be used to specify a formula and its result.
This function is rarely required and is only provided for compatibility with some third party applications. For most applications the worksheet_write_formula() function is the recommended way of writing formulas.
See also Working with Formulas.
lxw_error worksheet_write_formula_str | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | formula, | ||
lxw_format * | format, | ||
const char * | result | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
formula | Formula string to write to cell. |
format | A pointer to a Format instance or NULL. |
result | A user defined string result for the formula. |
The worksheet_write_formula_str()
function writes a formula or Excel function to the cell specified by row
and column
with a user defined string result:
The worksheet_write_formula_str()
function is similar to the worksheet_write_formula_num()
function except it writes a string result instead or a numeric result. See worksheet_write_formula_num()
for more details on why/when these functions are required.
One place where the worksheet_write_formula_str()
function may be required is to specify an empty result which will force a recalculation of the formula when loaded in LibreOffice.
See the FAQ Q. Why do my formulas show a zero result in some, non-Excel applications?.
See also Working with Formulas.
lxw_error worksheet_write_rich_string | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_rich_string_tuple * | rich_string[], | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
rich_string | An array of format/string lxw_rich_string_tuple fragments. |
format | A pointer to a Format instance or NULL. |
The worksheet_write_rich_string()
function is used to write strings with multiple formats. For example to write the string 'This is bold and this is italic' you would use the following:
The basic rule is to break the string into fragments and put a lxw_format object before the fragment that you want to format. So if we look at the above example again:
This is bold and this is italic
The would be broken down into 4 fragments:
default: |This is | bold: |bold| default: | and this is | italic: |italic|
This in then converted to the lxw_rich_string_tuple fragments shown in the example above. For the default format we use NULL
.
The fragments are passed to worksheet_write_rich_string()
as a NULL
terminated array:
Note: Excel doesn't allow the use of two consecutive formats in a rich string or an empty string fragment. For either of these conditions a warning is raised and the input to worksheet_write_rich_string()
is ignored.
lxw_error worksheet_write_comment | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | string | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
string | The comment string to be written. |
The worksheet_write_comment()
function is used to add a comment to a cell. A comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. Moving the cursor over the red triangle will reveal the comment.
The following example shows how to add a comment to a cell:
See also Working with Cell Comments
lxw_error worksheet_write_comment_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | string, | ||
lxw_comment_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
string | The comment string to be written. |
options | lxw_comment_options to control position and format of the comment. |
The worksheet_write_comment_opt()
function is used to add a comment to a cell with option that control the position, format and metadata of the comment. A comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. Moving the cursor over the red triangle will reveal the comment.
The following example shows how to add a comment to a cell with options:
The following options are available in lxw_comment_options:
author
visible
width
height
x_scale
y_scale
color
font_name
font_size
start_row
start_col
x_offset
y_offset
Comment options are explained in detail in the Setting Comment Properties section of the docs.
lxw_error worksheet_set_row | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
double | height, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
height | The row height, in character units. |
format | A pointer to a Format instance or NULL. |
The worksheet_set_row()
function is used to change the default properties of a row. The most common use for this function is to change the height of a row:
The height is specified in character units. To specify the height in pixels use the worksheet_set_row_pixels()
function.
The other common use for worksheet_set_row()
is to set the a Format for all cells in the row:
If you wish to set the format of a row without changing the height you can pass the default row height of LXW_DEF_ROW_HEIGHT = 15:
The format
parameter will be applied to any cells in the row that don't have a format. As with Excel the row format is overridden by an explicit cell format. For example:
lxw_error worksheet_set_row_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
double | height, | ||
lxw_format * | format, | ||
lxw_row_col_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
height | The row height. |
format | A pointer to a Format instance or NULL. |
options | Optional row parameters: hidden, level, collapsed. |
The worksheet_set_row_opt()
function is the same as worksheet_set_row()
with an additional options
parameter.
The options
parameter is a lxw_row_col_options struct. It has the following members:
hidden
level
collapsed
The "hidden"
option is used to hide a row. This can be used, for example, to hide intermediary steps in a complicated calculation:
The "hidden"
, "level"
, and "collapsed"
, options can also be used to create Outlines and Grouping. See Working with Outlines and Grouping.
lxw_error worksheet_set_row_pixels | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
uint32_t | pixels, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
pixels | The row height in pixels. |
format | A pointer to a Format instance or NULL. |
The worksheet_set_row_pixels()
function is the same as the worksheet_set_row()
function except that the height can be set in pixels
If you wish to set the format of a row without changing the height you can pass the default row height in pixels: LXW_DEF_ROW_HEIGHT_PIXELS.
lxw_error worksheet_set_row_pixels_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
uint32_t | pixels, | ||
lxw_format * | format, | ||
lxw_row_col_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
pixels | The row height in pixels. |
format | A pointer to a Format instance or NULL. |
options | Optional row parameters: hidden, level, collapsed. |
The worksheet_set_row_pixels_opt()
function is the same as the worksheet_set_row_opt()
function except that the height can be set in pixels.
lxw_error worksheet_set_column | ( | lxw_worksheet * | worksheet, |
lxw_col_t | first_col, | ||
lxw_col_t | last_col, | ||
double | width, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_col | The zero indexed first column. |
last_col | The zero indexed last column. |
width | The width of the column(s). |
format | A pointer to a Format instance or NULL. |
The worksheet_set_column()
function can be used to change the default properties of a single column or a range of columns:
If worksheet_set_column()
is applied to a single column the value of first_col
and last_col
should be the same:
It is also possible, and generally clearer, to specify a column range using the form of COLS()
macro:
The width
parameter sets the column width in the same units used by Excel which is: the number of characters in the default font. The default width is 8.43 in the default font of Calibri 11. The actual relationship between a string width and a column width in Excel is complex. See the following explanation of column widths from the Microsoft support documentation for more details. To set the width in pixels use the worksheet_set_column_pixels()
function.
There is no way to specify "AutoFit" for a column in the Excel file format. This feature is only available at runtime from within Excel. It is possible to simulate "AutoFit" in your application by tracking the maximum width of the data in the column as your write it and then adjusting the column width at the end.
As usual the format.h format
parameter is optional. If you wish to set the format without changing the width you can pass a default column width of LXW_DEF_COL_WIDTH = 8.43:
The format
parameter will be applied to any cells in the column that don't have a format. For example:
As in Excel a row format takes precedence over a default column format:
lxw_error worksheet_set_column_opt | ( | lxw_worksheet * | worksheet, |
lxw_col_t | first_col, | ||
lxw_col_t | last_col, | ||
double | width, | ||
lxw_format * | format, | ||
lxw_row_col_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_col | The zero indexed first column. |
last_col | The zero indexed last column. |
width | The width of the column(s). |
format | A pointer to a Format instance or NULL. |
options | Optional row parameters: hidden, level, collapsed. |
The worksheet_set_column_opt()
function is the same as worksheet_set_column()
with an additional options
parameter.
The options
parameter is a lxw_row_col_options struct. It has the following members:
hidden
level
collapsed
The "hidden"
option is used to hide a column. This can be used, for example, to hide intermediary steps in a complicated calculation:
The "hidden"
, "level"
, and "collapsed"
, options can also be used to create Outlines and Grouping. See Working with Outlines and Grouping.
lxw_error worksheet_set_column_pixels | ( | lxw_worksheet * | worksheet, |
lxw_col_t | first_col, | ||
lxw_col_t | last_col, | ||
uint32_t | pixels, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_col | The zero indexed first column. |
last_col | The zero indexed last column. |
pixels | The width of the column(s) in pixels. |
format | A pointer to a Format instance or NULL. |
The worksheet_set_column_pixels()
function is the same as worksheet_set_column()
function except that the width can be set in pixels:
If you wish to set the format of a column without changing the width you can pass the default column width in pixels: LXW_DEF_COL_WIDTH_PIXELS.
lxw_error worksheet_set_column_pixels_opt | ( | lxw_worksheet * | worksheet, |
lxw_col_t | first_col, | ||
lxw_col_t | last_col, | ||
uint32_t | pixels, | ||
lxw_format * | format, | ||
lxw_row_col_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_col | The zero indexed first column. |
last_col | The zero indexed last column. |
pixels | The width of the column(s) in pixels. |
format | A pointer to a Format instance or NULL. |
options | Optional row parameters: hidden, level, collapsed. |
The worksheet_set_column_pixels_opt()
function is the same as the worksheet_set_column_opt()
function except that the width can be set in pixels.
lxw_error worksheet_insert_image | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | filename | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
filename | The image filename, with path if required. |
This function can be used to insert a image into a worksheet. The image can be in PNG, JPEG, GIF or BMP format:
The worksheet_insert_image_opt()
function takes additional optional parameters to position and scale the image, see below.
Note: The scaling of a image may be affected if is crosses a row that has its default height changed due to a font that is larger than the default font size or that has text wrapping turned on. To avoid this you should explicitly set the height of the row using worksheet_set_row()
if it crosses an inserted image. See Working with Object Positioning.
NOTE on SVG files: Excel doesn't directly support SVG files in the same way as other image file formats. It allows SVG to be inserted into a worksheet but converts them to, and displays them as, PNG files. It stores the original SVG image in the file so the original format can be retrieved. This removes the file size and resolution advantage of using SVG files. As such SVG files are not supported by libxlsxwriter
since a conversion to the PNG format would be required and that format is already supported.
BMP images are only supported for backward compatibility. In general it is best to avoid BMP images since they aren't compressed. If used, BMP images must be 24 bit, true color, bitmaps.
lxw_error worksheet_insert_image_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | filename, | ||
lxw_image_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
filename | The image filename, with path if required. |
options | Optional image parameters. |
The worksheet_insert_image_opt()
function is like worksheet_insert_image()
function except that it takes an optional lxw_image_options struct with the following members/options:
x_offset
: Offset from the left of the cell in pixels.y_offset
: Offset from the top of the cell in pixels.x_scale
: X scale of the image as a decimal.y_scale
: Y scale of the image as a decimal.object_position
: See Working with Object Positioning.description
: Optional description or "Alt text" for the image.decorative
: Optional parameter to mark image as decorative.url
: Add an optional hyperlink to the image.tip
: Add an optional mouseover tip for a hyperlink to the image.For example, to scale and position the image:
The url
field of lxw_image_options can be use to used to add a hyperlink to an image:
The supported URL formats are the same as those supported by the worksheet_write_url()
method and the same rules/limits apply.
The tip
field of lxw_image_options can be use to used to add a mouseover tip to the hyperlink:
worksheet_insert_image()
above. lxw_error worksheet_insert_image_buffer | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const unsigned char * | image_buffer, | ||
size_t | image_size | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
image_buffer | Pointer to an array of bytes that holds the image data. |
image_size | The size of the array of bytes. |
This function can be used to insert a image into a worksheet from a memory buffer:
The buffer should be a pointer to an array of unsigned char data with a specified size.
See worksheet_insert_image()
for details about the supported image formats, and other image features.
lxw_error worksheet_insert_image_buffer_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const unsigned char * | image_buffer, | ||
size_t | image_size, | ||
lxw_image_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
image_buffer | Pointer to an array of bytes that holds the image data. |
image_size | The size of the array of bytes. |
options | Optional image parameters. |
The worksheet_insert_image_buffer_opt()
function is like worksheet_insert_image_buffer()
function except that it takes an optional lxw_image_options struct with the following members/options:
x_offset
: Offset from the left of the cell in pixels.y_offset
: Offset from the top of the cell in pixels.x_scale
: X scale of the image as a decimal.y_scale
: Y scale of the image as a decimal.object_position
: See Working with Object Positioning.description
: Optional description or "Alt text" for the image.decorative
: Optional parameter to mark image as decorative.url
: Add an optional hyperlink to the image.tip
: Add an optional mouseover tip for a hyperlink to the image.For example, to scale and position the image:
The buffer should be a pointer to an array of unsigned char data with a specified size.
See worksheet_insert_image_buffer_opt()
for details about the supported image formats, and other image options.
lxw_error worksheet_embed_image | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | filename | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
filename | The image filename, with path if required. |
This function can be used to embed a image into a worksheet cell and have the image automatically scale to the width and height of the cell. The X/Y scaling of the image is preserved but the size of the image is adjusted to fit the largest possible width or height depending on the cell dimensions.
This is the equivalent of Excel's menu option to insert an image using the option to "Place in Cell" which is only available in Excel 365 versions from 2023 onwards. For older versions of Excel a #VALUE!
error is displayed.
The worksheet_embed_image_opt()
function takes additional optional parameters to add urls or format the cell background, see below.
lxw_error worksheet_embed_image_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const char * | filename, | ||
lxw_image_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
filename | The image filename, with path if required. |
options | Optional image parameters. |
The worksheet_embed_image_opt()
function is like worksheet_embed_image()
function except that it takes an optional lxw_image_options struct with the following members/options:
description
: Optional description or "Alt text" for the image.decorative
: Optional parameter to mark image as decorative.url
: Add an optional hyperlink to the image.cell_format
: Add a format for the cell behind the embedded image. lxw_error worksheet_embed_image_buffer | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const unsigned char * | image_buffer, | ||
size_t | image_size | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
image_buffer | Pointer to an array of bytes that holds the image data. |
image_size | The size of the array of bytes. |
This function can be used to embed a image into a worksheet from a memory buffer:
The buffer should be a pointer to an array of unsigned char data with a specified size.
See worksheet_embed_image()
for details about the supported image formats, and other image features.
lxw_error worksheet_embed_image_buffer_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
const unsigned char * | image_buffer, | ||
size_t | image_size, | ||
lxw_image_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
image_buffer | Pointer to an array of bytes that holds the image data. |
image_size | The size of the array of bytes. |
options | Optional image parameters. |
The worksheet_embed_image_buffer_opt()
function is like worksheet_embed_image_buffer()
function except that it takes an optional lxw_image_options struct with the following members/options:
description
: Optional description or "Alt text" for the image.decorative
: Optional parameter to mark image as decorative.url
: Add an optional hyperlink to the image.cell_format
: Add a format for the cell behind the embedded image. lxw_error worksheet_set_background | ( | lxw_worksheet * | worksheet, |
const char * | filename | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
filename | The image filename, with path if required. |
The worksheet_set_background()
function can be used to set the background image for a worksheet:
The set_background()
method supports all the image formats supported by worksheet_insert_image()
.
Some people use this method to add a watermark background to their document. However, Microsoft recommends using a header image to set a watermark. The choice of method depends on whether you want the watermark to be visible in normal viewing mode or just when the file is printed. In libxlsxwriter you can get the header watermark effect using worksheet_set_header()
:
lxw_error worksheet_set_background_buffer | ( | lxw_worksheet * | worksheet, |
const unsigned char * | image_buffer, | ||
size_t | image_size | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
image_buffer | Pointer to an array of bytes that holds the image data. |
image_size | The size of the array of bytes. |
This function can be used to insert a background image into a worksheet from a memory buffer:
The buffer should be a pointer to an array of unsigned char data with a specified size.
See worksheet_set_background()
for more details.
lxw_error worksheet_insert_chart | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_chart * | chart | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
chart | A lxw_chart object created via workbook_add_chart(). |
The worksheet_insert_chart()
function can be used to insert a chart into a worksheet. The chart object must be created first using the workbook_add_chart()
function and configured using the chart.h functions.
Note:
A chart may only be inserted into a worksheet once. If several similar charts are required then each one must be created separately with worksheet_insert_chart()
.
lxw_error worksheet_insert_chart_opt | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_chart * | chart, | ||
lxw_chart_options * | user_options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
chart | A lxw_chart object created via workbook_add_chart(). |
user_options | Optional chart parameters. |
The worksheet_insert_chart_opt()
function is like worksheet_insert_chart()
function except that it takes an optional lxw_chart_options struct to scale and position the chart:
lxw_error worksheet_merge_range | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col, | ||
const char * | string, | ||
lxw_format * | format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
string | String to write to the merged range. |
format | A pointer to a Format instance or NULL. |
The worksheet_merge_range()
function allows cells to be merged together so that they act as a single area.
Excel generally merges and centers cells at same time. To get similar behavior with libxlsxwriter you need to apply a Format object with the appropriate alignment:
It is possible to apply other formatting to the merged cells as well:
The worksheet_merge_range()
function writes a char*
string using worksheet_write_string()
. In order to write other data types, such as a number or a formula, you can overwrite the first cell with a call to one of the other write functions. The same Format should be used as was used in the merged range.
constant_memory
mode is enabled. lxw_error worksheet_autofilter | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
The worksheet_autofilter()
function allows an autofilter to be added to a worksheet.
An autofilter is a way of adding dropdown lists to the headers of a 2D range of worksheet data. This allows users to filter the data based on simple criteria so that some data is shown and some is hidden.
To add an autofilter to a worksheet:
In order to apply a filter condition it is necessary to add filter rules to the columns using either the worksheet_filter_column()
, worksheet_filter_column2()
or worksheet_filter_list()
functions:
worksheet_filter_column()
: filter on a single criterion such as "Column ==
East". More complex conditions such as "<=" or ">=" can also be use.worksheet_filter_column2()
: filter on two criteria such as "Column == East
or Column == West". Complex conditions can also be used.worksheet_filter_list()
: filter on a list of values such as "Column in (East, West,
North)".These functions are explained below. It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. See Filtering data in an autofilter for more details.
lxw_error worksheet_filter_column | ( | lxw_worksheet * | worksheet, |
lxw_col_t | col, | ||
lxw_filter_rule * | rule | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
col | The column in the autofilter that the rule applies to. |
rule | The lxw_filter_rule autofilter rule. |
The worksheet_filter_column
function can be used to filter columns in a autofilter range based on single rule conditions:
The rules and criteria are explained in more detail in Setting a filter criteria for a column in Working with Autofilters.
The col
parameter is a zero indexed column number and must refer to a column in an existing autofilter created with worksheet_autofilter()
.
It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. See Filtering data in an autofilter for more details.
lxw_error worksheet_filter_column2 | ( | lxw_worksheet * | worksheet, |
lxw_col_t | col, | ||
lxw_filter_rule * | rule1, | ||
lxw_filter_rule * | rule2, | ||
uint8_t | and_or | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
col | The column in the autofilter that the rules applies to. |
rule1 | First lxw_filter_rule autofilter rule. |
rule2 | Second lxw_filter_rule autofilter rule. |
and_or | A lxw_filter_operator and/or operator. |
The worksheet_filter_column2
function can be used to filter columns in a autofilter range based on two rule conditions:
The rules and criteria are explained in more detail in Setting a filter criteria for a column in Working with Autofilters.
The col
parameter is a zero indexed column number and must refer to a column in an existing autofilter created with worksheet_autofilter()
.
The and_or
parameter is either "and (LXW_FILTER_AND)" or "or (LXW_FILTER_OR)".
It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. See Filtering data in an autofilter for more details.
lxw_error worksheet_filter_list | ( | lxw_worksheet * | worksheet, |
lxw_col_t | col, | ||
const char ** | list | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
col | The column in the autofilter that the rules applies to. |
list | A NULL terminated array of strings to filter on. |
The worksheet_filter_column_list()
function can be used specify multiple string matching criteria. This is a newer type of filter introduced in Excel 2007. Prior to that it was only possible to have either 1 or 2 filter conditions, such as the ones used by worksheet_filter_column()
and worksheet_filter_column2()
.
As an example, consider a column that contains data for the months of the year. The worksheet_filter_list()
function can be used to filter out data rows for different months:
Note, the array must be NULL terminated to indicate the end of the array of strings. To filter blanks as part of the list use Blanks
as a list item:
It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. See Filtering data in an autofilter for more details.
lxw_error worksheet_data_validation_cell | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_data_validation * | validation | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
validation | A lxw_data_validation object to control the validation. |
The worksheet_data_validation_cell()
function is used to construct an Excel data validation or to limit the user input to a dropdown list of values:
Data validation and the various options of lxw_data_validation are described in more detail in Working with Data Validation.
lxw_error worksheet_data_validation_range | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col, | ||
lxw_data_validation * | validation | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
validation | A lxw_data_validation object to control the validation. |
The worksheet_data_validation_range()
function is the same as the worksheet_data_validation_cell()
, see above, except the data validation is applied to a range of cells:
Data validation and the various options of lxw_data_validation are described in more detail in Working with Data Validation.
lxw_error worksheet_conditional_format_cell | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_conditional_format * | conditional_format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
conditional_format | A lxw_conditional_format object to control the conditional format. |
The worksheet_conditional_format_cell()
function is used to set a conditional format for a cell in a worksheet:
The conditional format parameters is specified in lxw_conditional_format.
See Working with Conditional Formatting for full details.
lxw_error worksheet_conditional_format_range | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col, | ||
lxw_conditional_format * | conditional_format | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
conditional_format | A lxw_conditional_format object to control the conditional format. |
The worksheet_conditional_format_cell()
function is used to set a conditional format for a range of cells in a worksheet:
Output:
The conditional format parameters is specified in lxw_conditional_format.
See Working with Conditional Formatting for full details.
lxw_error worksheet_insert_button | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col, | ||
lxw_button_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The zero indexed row number. |
col | The zero indexed column number. |
options | A lxw_button_options object to set the button properties. |
The worksheet_insert_button()
function can be used to insert an Excel form button into a worksheet. This function is generally only useful when used in conjunction with the workbook_add_vba_project()
function to tie the button to a macro from an embedded VBA project:
The button properties are set using the lxw_button_options struct.
See also Working with VBA Macros
lxw_error worksheet_add_table | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col, | ||
lxw_table_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
options | A lxw_table_options struct to define the table options. |
The worksheet_add_table()
function is used to add a table to a worksheet. Tables in Excel are a way of grouping a range of cells into a single entity that has common formatting or that can be referenced from formulas. Tables can have column headers, autofilters, total rows, column formulas and default formatting.
Output:
See Working with Worksheet Tables for more detailed usage information and also tables.c.
void worksheet_activate | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_activate()
function is used to specify which worksheet is initially visible in a multi-sheet workbook:
More than one worksheet can be selected via the worksheet_select()
function, see below, however only one worksheet can be active.
The default active worksheet is the first worksheet.
void worksheet_select | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_select()
function is used to indicate that a worksheet is selected in a multi-sheet workbook:
A selected worksheet has its tab highlighted. Selecting worksheets is a way of grouping them together so that, for example, several worksheets could be printed in one go. A worksheet that has been activated via the worksheet_activate()
function will also appear as selected.
void worksheet_hide | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_hide()
function is used to hide a worksheet:
You may wish to hide a worksheet in order to avoid confusing a user with intermediate data or calculations.
A hidden worksheet can not be activated or selected so this function is mutually exclusive with the worksheet_activate()
and worksheet_select()
functions. In addition, since the first worksheet will default to being the active worksheet, you cannot hide the first worksheet without activating another sheet:
void worksheet_set_first_sheet | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_activate()
function determines which worksheet is initially selected. However, if there are a large number of worksheets the selected worksheet may not appear on the screen. To avoid this you can select the leftmost visible worksheet tab using worksheet_set_first_sheet()
:
This function is not required very often. The default value is the first worksheet.
void worksheet_freeze_panes | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The cell row (zero indexed). |
col | The cell column (zero indexed). |
The worksheet_freeze_panes()
function can be used to divide a worksheet into horizontal or vertical regions known as panes and to "freeze" these panes so that the splitter bars are not visible.
The parameters row
and col
are used to specify the location of the split. It should be noted that the split is specified at the top or left of a cell and that the function uses zero based indexing. Therefore to freeze the first row of a worksheet it is necessary to specify the split at row 2 (which is 1 as the zero-based index).
You can set one of the row
and col
parameters as zero if you do not want either a vertical or horizontal split.
Examples:
void worksheet_split_panes | ( | lxw_worksheet * | worksheet, |
double | vertical, | ||
double | horizontal | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
vertical | The position for the vertical split. |
horizontal | The position for the horizontal split. |
The worksheet_split_panes()
function can be used to divide a worksheet into horizontal or vertical regions known as panes. This function is different from the worksheet_freeze_panes()
function in that the splits between the panes will be visible to the user and each pane will have its own scroll bars.
The parameters vertical
and horizontal
are used to specify the vertical and horizontal position of the split. The units for vertical
and horizontal
are the same as those used by Excel to specify row height and column width. However, the vertical and horizontal units are different from each other. Therefore you must specify the vertical
and horizontal
parameters in terms of the row heights and column widths that you have set or the default values which are 15 for a row and 8.43 for a column.
Examples:
lxw_error worksheet_set_selection | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col | ||
) |
worksheet | A pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
The worksheet_set_selection()
function can be used to specify which cell or range of cells is selected in a worksheet: The most common requirement is to select a single cell, in which case the first_
and last_
parameters should be the same.
The active cell within a selected range is determined by the order in which first_
and last_
are specified.
Examples:
void worksheet_set_top_left_cell | ( | lxw_worksheet * | worksheet, |
lxw_row_t | row, | ||
lxw_col_t | col | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
row | The cell row (zero indexed). |
col | The cell column (zero indexed). |
The worksheet_set_top_left_cell()
function can be used to set the top leftmost visible cell in the worksheet:
void worksheet_set_landscape | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
This function is used to set the orientation of a worksheet's printed page to landscape:
void worksheet_set_portrait | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
This function is used to set the orientation of a worksheet's printed page to portrait. The default worksheet orientation is portrait, so this function isn't generally required:
void worksheet_set_page_view | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
This function is used to display the worksheet in "Page View/Layout" mode:
void worksheet_set_paper | ( | lxw_worksheet * | worksheet, |
uint8_t | paper_type | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
paper_type | The Excel paper format type. |
This function is used to set the paper format for the printed output of a worksheet. The following paper styles are available:
Index | Paper format | Paper size |
---|---|---|
0 | Printer default | Printer default |
1 | Letter | 8 1/2 x 11 in |
2 | Letter Small | 8 1/2 x 11 in |
3 | Tabloid | 11 x 17 in |
4 | Ledger | 17 x 11 in |
5 | Legal | 8 1/2 x 14 in |
6 | Statement | 5 1/2 x 8 1/2 in |
7 | Executive | 7 1/4 x 10 1/2 in |
8 | A3 | 297 x 420 mm |
9 | A4 | 210 x 297 mm |
10 | A4 Small | 210 x 297 mm |
11 | A5 | 148 x 210 mm |
12 | B4 | 250 x 354 mm |
13 | B5 | 182 x 257 mm |
14 | Folio | 8 1/2 x 13 in |
15 | Quarto | 215 x 275 mm |
16 | — | 10x14 in |
17 | — | 11x17 in |
18 | Note | 8 1/2 x 11 in |
19 | Envelope 9 | 3 7/8 x 8 7/8 |
20 | Envelope 10 | 4 1/8 x 9 1/2 |
21 | Envelope 11 | 4 1/2 x 10 3/8 |
22 | Envelope 12 | 4 3/4 x 11 |
23 | Envelope 14 | 5 x 11 1/2 |
24 | C size sheet | — |
25 | D size sheet | — |
26 | E size sheet | — |
27 | Envelope DL | 110 x 220 mm |
28 | Envelope C3 | 324 x 458 mm |
29 | Envelope C4 | 229 x 324 mm |
30 | Envelope C5 | 162 x 229 mm |
31 | Envelope C6 | 114 x 162 mm |
32 | Envelope C65 | 114 x 229 mm |
33 | Envelope B4 | 250 x 353 mm |
34 | Envelope B5 | 176 x 250 mm |
35 | Envelope B6 | 176 x 125 mm |
36 | Envelope | 110 x 230 mm |
37 | Monarch | 3.875 x 7.5 in |
38 | Envelope | 3 5/8 x 6 1/2 in |
39 | Fanfold | 14 7/8 x 11 in |
40 | German Std Fanfold | 8 1/2 x 12 in |
41 | German Legal Fanfold | 8 1/2 x 13 in |
Note, it is likely that not all of these paper types will be available to the end user since it will depend on the paper formats that the user's printer supports. Therefore, it is best to stick to standard paper types:
If you do not specify a paper type the worksheet will print using the printer's default paper style.
void worksheet_set_margins | ( | lxw_worksheet * | worksheet, |
double | left, | ||
double | right, | ||
double | top, | ||
double | bottom | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
left | Left margin in inches. Excel default is 0.7. |
right | Right margin in inches. Excel default is 0.7. |
top | Top margin in inches. Excel default is 0.75. |
bottom | Bottom margin in inches. Excel default is 0.75. |
The worksheet_set_margins()
function is used to set the margins of the worksheet when it is printed. The units are in inches. Specifying -1
for any parameter will give the default Excel value as shown above.
lxw_error worksheet_set_header | ( | lxw_worksheet * | worksheet, |
const char * | string | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
string | The header string. |
Headers and footers are generated using a string which is a combination of plain text and control characters.
The available control character are:
Control | Category | Description |
---|---|---|
&L | Justification | Left |
&C | Center | |
&R | Right | |
&P | Information | Page number |
&N | Total number of pages | |
&D | Date | |
&T | Time | |
&F | File name | |
&A | Worksheet name | |
&Z | Workbook path | |
&fontsize | Font | Font size |
&"font,style" | Font name and style | |
&U | Single underline | |
&E | Double underline | |
&S | Strikethrough | |
&X | Superscript | |
&Y | Subscript | |
&[Picture] | Images | Image placeholder |
&G | Same as &[Picture] | |
&& | Miscellaneous | Literal ampersand & |
Note: inserting images requires the worksheet_set_header_opt()
function.
Text in headers and footers can be justified (aligned) to the left, center and right by prefixing the text with the control characters &L
, &C
and &R
.
For example (with ASCII art representation of the results):
For simple text, if you do not specify any justification the text will be centered. However, you must prefix the text with &C
if you specify a font name or any other formatting:
You can have text in each of the justification regions:
The information control characters act as variables that Excel will update as the workbook or worksheet changes. Times and dates are in the users default format:
You can specify the font size of a section of the text by prefixing it with the control character &n
where n
is the font size:
You can specify the font of a section of the text by prefixing it with the control sequence &"font,style"
where fontname
is a font name such as Windows font descriptions: "Regular", "Italic", "Bold" or "Bold Italic": "Courier New" or "Times New Roman" and style
is one of the standard
It is possible to combine all of these features together to create sophisticated headers and footers. As an aid to setting up complicated headers and footers you can record a page set-up as a macro in Excel and look at the format strings that VBA produces. Remember however that VBA uses two double quotes ""
to indicate a single double quote. For the last example above the equivalent VBA code looks like this:
Alternatively you can inspect the header and footer strings in an Excel file by unzipping it and grepping the XML sub-files. The following shows how to do that using libxml's xmllint to format the XML for clarity:
To include a single literal ampersand &
in a header or footer you should use a double ampersand &&
:
lxw_error worksheet_set_footer | ( | lxw_worksheet * | worksheet, |
const char * | string | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
string | The footer string. |
The syntax of this function is the same as worksheet_set_header().
lxw_error worksheet_set_header_opt | ( | lxw_worksheet * | worksheet, |
const char * | string, | ||
lxw_header_footer_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
string | The header string. |
options | Header options. |
The syntax of this function is the same as worksheet_set_header()
with an additional parameter to specify options for the header.
The lxw_header_footer_options options are:
margin
: Header or footer margin in inches. The value must by larger than 0.0. The Excel default is 0.3.image_left
: The left header image filename, with path if required. This should have a corresponding &G/&[Picture]
placeholder in the &L
section of the header/footer string.image_center
: The center header image filename, with path if required. This should have a corresponding &G/&[Picture]
placeholder in the &C
section of the header/footer string.image_right
: The right header image filename, with path if required. This should have a corresponding &G/&[Picture]
placeholder in the &R
section of the header/footer string.Images can be inserted in the header by specifying the &[Picture]
placeholder and a filename/path to the image:
lxw_error worksheet_set_footer_opt | ( | lxw_worksheet * | worksheet, |
const char * | string, | ||
lxw_header_footer_options * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
string | The footer string. |
options | Footer options. |
The syntax of this function is the same as worksheet_set_header_opt()
.
lxw_error worksheet_set_h_pagebreaks | ( | lxw_worksheet * | worksheet, |
lxw_row_t | breaks[] | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
breaks | Array of page breaks. |
The worksheet_set_h_pagebreaks()
function adds horizontal page breaks to a worksheet. A page break causes all the data that follows it to be printed on the next page. Horizontal page breaks act between rows.
The function takes an array of one or more page breaks. The type of the array data is lxw_row_t and the last element of the array must be 0:
To create a page break between rows 20 and 21 you must specify the break at row 21. However in zero index notation this is actually row 20:
There is an Excel limitation of 1023 horizontal page breaks per worksheet.
Note: If you specify the "fit to page" option via the worksheet_fit_to_pages()
function it will override all manual page breaks.
lxw_error worksheet_set_v_pagebreaks | ( | lxw_worksheet * | worksheet, |
lxw_col_t | breaks[] | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
breaks | Array of page breaks. |
The worksheet_set_v_pagebreaks()
function adds vertical page breaks to a worksheet. A page break causes all the data that follows it to be printed on the next page. Vertical page breaks act between columns.
The function takes an array of one or more page breaks. The type of the array data is lxw_col_t and the last element of the array must be 0:
To create a page break between columns 20 and 21 you must specify the break at column 21. However in zero index notation this is actually column 20:
There is an Excel limitation of 1023 vertical page breaks per worksheet.
Note: If you specify the "fit to page" option via the worksheet_fit_to_pages()
function it will override all manual page breaks.
void worksheet_print_across | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_print_across()
function is used to change the default print direction. This is referred to by Excel as the sheet "page order":
The default page order is shown below for a worksheet that extends over 4 pages. The order is called "down then across":
[1] [3] [2] [4]
However, by using the print_across
function the print order will be changed to "across then down":
[1] [2] [3] [4]
void worksheet_set_zoom | ( | lxw_worksheet * | worksheet, |
uint16_t | scale | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
scale | Worksheet zoom factor. |
Set the worksheet zoom factor in the range 10 <= zoom <= 400
:
The default zoom factor is 100. It isn't possible to set the zoom to "Selection" because it is calculated by Excel at run-time.
Note, worksheet_zoom()
does not affect the scale of the printed page. For that you should use worksheet_set_print_scale()
.
void worksheet_gridlines | ( | lxw_worksheet * | worksheet, |
uint8_t | option | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
option | Gridline option. |
Display or hide screen and print gridlines using one of the values of lxw_gridlines.
The Excel default is that the screen gridlines are on and the printed worksheet is off.
void worksheet_center_horizontally | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
Center the worksheet data horizontally between the margins on the printed page:
void worksheet_center_vertically | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
Center the worksheet data vertically between the margins on the printed page:
void worksheet_print_row_col_headers | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
When printing a worksheet from Excel the row and column headers (the row numbers on the left and the column letters at the top) aren't printed by default.
This function sets the printer option to print these headers:
lxw_error worksheet_repeat_rows | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_row_t | last_row | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | First row of repeat range. |
last_row | Last row of repeat range. |
For large Excel documents it is often desirable to have the first row or rows of the worksheet print out at the top of each page.
This can be achieved by using this function. The parameters first_row
and last_row
are zero based:
lxw_error worksheet_repeat_columns | ( | lxw_worksheet * | worksheet, |
lxw_col_t | first_col, | ||
lxw_col_t | last_col | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_col | First column of repeat range. |
last_col | Last column of repeat range. |
For large Excel documents it is often desirable to have the first column or columns of the worksheet print out at the left of each page.
This can be achieved by using this function. The parameters first_col
and last_col
are zero based:
lxw_error worksheet_print_area | ( | lxw_worksheet * | worksheet, |
lxw_row_t | first_row, | ||
lxw_col_t | first_col, | ||
lxw_row_t | last_row, | ||
lxw_col_t | last_col | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
first_row | The first row of the range. (All zero indexed.) |
first_col | The first column of the range. |
last_row | The last row of the range. |
last_col | The last col of the range. |
This function is used to specify the area of the worksheet that will be printed. The RANGE() macro is often convenient for this.
In order to set a row or column range you must specify the entire range:
void worksheet_fit_to_pages | ( | lxw_worksheet * | worksheet, |
uint16_t | width, | ||
uint16_t | height | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
width | Number of pages horizontally. |
height | Number of pages vertically. |
The worksheet_fit_to_pages()
function is used to fit the printed area to a specific number of pages both vertically and horizontally. If the printed area exceeds the specified number of pages it will be scaled down to fit. This ensures that the printed area will always appear on the specified number of pages even if the page size or margins change:
The print area can be defined using the worksheet_print_area()
function as described above.
A common requirement is to fit the printed output to n
pages wide but have the height be as long as necessary. To achieve this set the height
to zero:
Note:
worksheet_fit_to_pages()
and worksheet_set_print_scale()
on the same worksheet Excel only allows one of these options to be active at a time. The last function call made will set the active option.worksheet_fit_to_pages()
function will override any manual page breaks that are defined in the worksheet.worksheet_fit_to_pages()
it may also be required to set the printer paper size using worksheet_set_paper()
or else Excel will default to "US Letter". void worksheet_set_start_page | ( | lxw_worksheet * | worksheet, |
uint16_t | start_page | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
start_page | Page number of the starting page when printing. |
The worksheet_set_start_page()
function is used to set the number number of the first page when the worksheet is printed out. It is the same as the "First Page Number" option in Excel:
void worksheet_set_print_scale | ( | lxw_worksheet * | worksheet, |
uint16_t | scale | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
scale | Print scale of worksheet to be printed. |
This function sets the scale factor of the printed page. The Scale factor must be in the range 10 <= scale <= 400
:
The default scale factor is 100. Note, worksheet_set_print_scale()
does not affect the scale of the visible page in Excel. For that you should use worksheet_set_zoom()
.
Note that although it is valid to use both worksheet_fit_to_pages()
and worksheet_set_print_scale()
on the same worksheet Excel only allows one of these options to be active at a time. The last function call made will set the active option.
void worksheet_print_black_and_white | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
Set the option to print the worksheet in black and white:
void worksheet_right_to_left | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_right_to_left()
function is used to change the default direction of the worksheet from left-to-right, with the A1
cell in the top left, to right-to-left, with the A1
cell in the top right.
This is useful when creating Arabic, Hebrew or other near or far eastern worksheets that use right-to-left as the default direction.
void worksheet_hide_zero | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
The worksheet_hide_zero()
function is used to hide any zero values that appear in cells:
void worksheet_set_tab_color | ( | lxw_worksheet * | worksheet, |
lxw_color_t | color | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
color | The tab color. |
The worksheet_set_tab_color()
function is used to change the color of the worksheet tab:
The color should be an RGB integer value, see Working with Colors.
void worksheet_protect | ( | lxw_worksheet * | worksheet, |
const char * | password, | ||
lxw_protection * | options | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
password | A worksheet password. |
options | Worksheet elements to protect. |
The worksheet_protect()
function protects worksheet elements from modification:
The password
and lxw_protection pointer are both optional:
Passing a NULL
password is the same as turning on protection without a password. Passing a NULL
password and NULL
options, or any other combination has the effect of enabling a cell's locked
and hidden
properties if they have been set.
A locked cell cannot be edited and this property is on by default for all cells. A hidden cell will display the results of a formula but not the formula itself. These properties can be set using the format_set_unlocked() and format_set_hidden() format functions.
You can specify which worksheet elements you wish to protect by passing a lxw_protection pointer in the options
argument with any or all of the following members set:
no_select_locked_cells no_select_unlocked_cells format_cells format_columns format_rows insert_columns insert_rows insert_hyperlinks delete_columns delete_rows sort autofilter pivot_tables scenarios objects
All parameters are off by default. Individual elements can be protected as follows:
See also the format_set_unlocked() and format_set_hidden() format functions.
Note: Sheet level passwords in Excel offer very weak protection. They don't encrypt your data and are very easy to deactivate. Full workbook encryption is not supported by libxlsxwriter
since it requires a completely different file format.
void worksheet_outline_settings | ( | lxw_worksheet * | worksheet, |
uint8_t | visible, | ||
uint8_t | symbols_below, | ||
uint8_t | symbols_right, | ||
uint8_t | auto_style | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
visible | Outlines are visible. Optional, defaults to True. |
symbols_below | Show row outline symbols below the outline bar. |
symbols_right | Show column outline symbols to the right of outline. |
auto_style | Use Automatic outline style. |
The worksheet_outline_settings()
method is used to control the appearance of outlines in Excel. Outlines are described the section on Working with Outlines and Grouping.
The visible
parameter is used to control whether or not outlines are visible. Setting this parameter to False will cause all outlines on the worksheet to be hidden. They can be un-hidden in Excel by means of the "Show Outline Symbols" command button. The default Excel setting is True for visible outlines.
The symbols_below
parameter is used to control whether the row outline symbol will appear above or below the outline level bar. The default Excel setting is True for symbols to appear below the outline level bar.
The symbols_right
parameter is used to control whether the column outline symbol will appear to the left or the right of the outline level bar. The default Excel setting is True for symbols to appear to the right of the outline level bar.
The auto_style
parameter is used to control whether the automatic outline generator in Excel uses automatic styles when creating an outline. This has no effect on a file generated by XlsxWriter but it does have an effect on how the worksheet behaves after it is created. The default Excel setting is False for "Automatic Styles" to be turned off.
The default settings for all of these parameters in libxlsxwriter correspond to Excel's default parameters and are shown below:
The worksheet parameters controlled by worksheet_outline_settings()
are rarely used.
void worksheet_set_default_row | ( | lxw_worksheet * | worksheet, |
double | height, | ||
uint8_t | hide_unused_rows | ||
) |
worksheet | Pointer to a lxw_worksheet instance to be updated. |
height | Default row height. |
hide_unused_rows | Hide unused cells. |
The worksheet_set_default_row()
function is used to set Excel default row properties such as the default height and the option to hide unused rows. These parameters are an optimization used by Excel to set row properties without generating a very large file with an entry for each row.
To set the default row height:
To hide unused rows:
Note, in the previous case we use the default height LXW_DEF_ROW_HEIGHT = 15 so the the height remains unchanged.
lxw_error worksheet_set_vba_name | ( | lxw_worksheet * | worksheet, |
const char * | name | ||
) |
worksheet | Pointer to a lxw_worksheet instance. |
name | Name of the worksheet used by VBA. |
The worksheet_set_vba_name()
function can be used to set the VBA name for the worksheet. This is sometimes required when a vbaProject macro included via workbook_add_vba_project()
refers to the worksheet by a name other than the worksheet name:
In general Excel uses the worksheet name such as "Sheet1" as the VBA name. However, this can be changed in the VBA environment or if the the macro was extracted from a foreign language version of Excel.
See also Working with VBA Macros
void worksheet_show_comments | ( | lxw_worksheet * | worksheet | ) |
worksheet | Pointer to a lxw_worksheet instance. |
This worksheet_show_comments()
function is used to make all cell comments visible when a worksheet is opened:
Individual comments can be made visible or hidden using the visible
option of the lxw_comment_options struct and the worksheet_write_comment_opt()
function (see above and Cell comments: visible).
void worksheet_set_comments_author | ( | lxw_worksheet * | worksheet, |
const char * | author | ||
) |
worksheet | Pointer to a lxw_worksheet instance. |
author | The name of the comment author. |
This worksheet_set_comments_author()
function is used to set the default author of all cell comments:
Individual authors can be set using the author
option of the lxw_comment_options struct and the worksheet_write_comment_opt()
function (see above and Cell comments: author).
lxw_error worksheet_ignore_errors | ( | lxw_worksheet * | worksheet, |
uint8_t | type, | ||
const char * | range | ||
) |
worksheet | Pointer to a lxw_worksheet instance. |
type | The type of error/warning to ignore. See lxw_ignore_errors. |
range | The range(s) for which the error/warning should be ignored. |
The worksheet_ignore_errors()
function can be used to ignore various worksheet cell errors/warnings. For example the following code writes a string that looks like a number:
This causes Excel to display a small green triangle in the top left hand corner of the cell to indicate an error/warning:
Sometimes these warnings are useful indicators that there is an issue in the spreadsheet but sometimes it is preferable to turn them off. Warnings can be turned off at the Excel level for all workbooks and worksheets by using the using "Excel options -> Formulas -> Error checking
rules". Alternatively you can turn them off for individual cells in a worksheet, or ranges of cells, using the worksheet_ignore_errors()
function with different lxw_ignore_errors options and ranges like this:
The range can be a single cell, a range of cells, or multiple cells and ranges separated by spaces:
worksheet_ignore_errors()
more than once for the same lxw_ignore_errors type will overwrite the previous range.You can turn off warnings for an entire column by specifying the range from the first cell in the column to the last cell in the column:
Or for the entire worksheet by specifying the range from the first cell in the worksheet to the last cell in the worksheet:
The worksheet errors/warnings that can be ignored are:
typedef struct lxw_row_col_options lxw_row_col_options |
Options struct for the worksheet_set_column() and worksheet_set_row() functions.
It has the following members:
hidden
level
collapsed
The members of this struct are explained in Outlines and Grouping in libxlsxwriter.
typedef struct lxw_conditional_format lxw_conditional_format |
The fields/options in the the lxw_conditional_format are used to define a worksheet conditional format. It is used in conjunction with worksheet_conditional_format()
.
typedef struct lxw_table_column lxw_table_column |
Structure to set the options of a table column added with worksheet_add_table(). See Parameter: columns.
typedef struct lxw_table_options lxw_table_options |
Options used to define worksheet tables. See Working with Worksheet Tables for more information.
typedef struct lxw_filter_rule lxw_filter_rule |
Options to define an autofilter rule.
typedef struct lxw_image_options lxw_image_options |
Options for modifying images inserted via worksheet_insert_image_opt()
and worksheet_embed_image_opt()
.
typedef struct lxw_chart_options lxw_chart_options |
Options for modifying charts inserted via worksheet_insert_chart_opt()
.
typedef struct lxw_comment_options lxw_comment_options |
Options for modifying comments inserted via worksheet_write_comment_opt()
.
typedef struct lxw_button_options lxw_button_options |
Options for modifying buttons inserted via worksheet_insert_button()
.
typedef struct lxw_header_footer_options lxw_header_footer_options |
Optional parameters used in the worksheet_set_header_opt()
and worksheet_set_footer_opt() functions.
typedef struct lxw_rich_string_tuple lxw_rich_string_tuple |
Arrays of this struct are used to define "rich" multi-format strings that are passed to worksheet_write_rich_string()
. Each struct represents a fragment of the rich multi-format string with a lxw_format to define the format for the string part. If the string fragment is unformatted then NULL
can be used for the format.
typedef struct lxw_worksheet lxw_worksheet |
The members of the lxw_worksheet struct aren't modified directly. Instead the worksheet properties are set by calling the functions shown in worksheet.h.
#define LXW_DEF_COL_WIDTH (double)8.43 |
Default Excel column width in character units.
#define LXW_DEF_ROW_HEIGHT (double)15.0 |
Default Excel column height in character units.
#define LXW_DEF_COL_WIDTH_PIXELS 64 |
Default Excel column width in pixels.
#define LXW_DEF_ROW_HEIGHT_PIXELS 20 |
Default Excel column height in pixels.
enum lxw_gridlines |
Gridline options using in worksheet_gridlines()
.
enum lxw_validation_types |
Data validation types.
Enumerator | |
---|---|
LXW_VALIDATION_TYPE_INTEGER | Restrict cell input to whole/integer numbers only. |
LXW_VALIDATION_TYPE_INTEGER_FORMULA | Restrict cell input to whole/integer numbers only, using a cell reference. |
LXW_VALIDATION_TYPE_DECIMAL | Restrict cell input to decimal numbers only. |
LXW_VALIDATION_TYPE_DECIMAL_FORMULA | Restrict cell input to decimal numbers only, using a cell reference. |
LXW_VALIDATION_TYPE_LIST | Restrict cell input to a list of strings in a dropdown. |
LXW_VALIDATION_TYPE_LIST_FORMULA | Restrict cell input to a list of strings in a dropdown, using a cell range. |
LXW_VALIDATION_TYPE_DATE | Restrict cell input to date values only, using a lxw_datetime type. |
LXW_VALIDATION_TYPE_DATE_FORMULA | Restrict cell input to date values only, using a cell reference. |
LXW_VALIDATION_TYPE_TIME | Restrict cell input to time values only, using a lxw_datetime type. |
LXW_VALIDATION_TYPE_TIME_FORMULA | Restrict cell input to time values only, using a cell reference. |
LXW_VALIDATION_TYPE_LENGTH | Restrict cell input to strings of defined length, using a cell reference. |
LXW_VALIDATION_TYPE_LENGTH_FORMULA | Restrict cell input to strings of defined length, using a cell reference. |
LXW_VALIDATION_TYPE_CUSTOM_FORMULA | Restrict cell to input controlled by a custom formula that returns |
LXW_VALIDATION_TYPE_ANY | Allow any type of input. Mainly only useful for pop-up messages. |
Data validation criteria uses to control the selection of data.
Data validation error types for pop-up messages.
Set the display type for a cell comment. This is hidden by default but can be set to visible with the worksheet_show_comments()
function.
Enumerator | |
---|---|
LXW_COMMENT_DISPLAY_DEFAULT | Default to the worksheet default which can be hidden or visible. |
LXW_COMMENT_DISPLAY_HIDDEN | Hide the cell comment. Usually the default. |
LXW_COMMENT_DISPLAY_VISIBLE | Show the cell comment. Can also be set for the worksheet with the |
Values used to set the "type" field of conditional format.
Criteria used to define how a conditional format works.
Conditional format rule types that apply to Color Scale and Data Bars.
Values used to set the bar direction of a conditional format data bar.
Values used to set the position of the axis in a conditional format data bar.
Definitions of icon styles used with Icon Set conditional formats.
enum lxw_table_style_type |
Definitions for the standard Excel functions that are available via the dropdown in the total row of an Excel table.
enum lxw_filter_criteria |
Criteria used to define an autofilter rule condition.
enum lxw_filter_operator |
And/or operator conditions when using 2 filter rules with worksheet_filter_column2(). In general LXW_FILTER_OR is used with LXW_FILTER_CRITERIA_EQUAL_TO and LXW_FILTER_AND is used with the other filter criteria.
Enumerator | |
---|---|
LXW_FILTER_AND | Logical "and" of 2 filter rules. |
LXW_FILTER_OR | Logical "or" of 2 filter rules. |
enum lxw_object_position |
Options to control the positioning of worksheet objects such as images or charts. See Working with Object Positioning.
Enumerator | |
---|---|
LXW_OBJECT_POSITION_DEFAULT | Default positioning for the object. |
LXW_OBJECT_MOVE_AND_SIZE | Move and size the worksheet object with the cells. |
LXW_OBJECT_MOVE_DONT_SIZE | Move but don't size the worksheet object with the cells. |
LXW_OBJECT_DONT_MOVE_DONT_SIZE | Don't move or size the worksheet object with the cells. |
LXW_OBJECT_MOVE_AND_SIZE_AFTER | Same as LXW_OBJECT_MOVE_AND_SIZE except libxlsxwriter applies hidden cells after the object is inserted. |
enum lxw_ignore_errors |
Options for ignoring worksheet errors/warnings. See worksheet_ignore_errors().
Data Structures | |
struct | lxw_row_col_options |
Options for rows and columns. More... | |
struct | lxw_data_validation |
Worksheet data validation options. More... | |
struct | lxw_conditional_format |
Worksheet conditional formatting options. More... | |
struct | lxw_table_column |
Table columns options. More... | |
struct | lxw_table_options |
Worksheet table options. More... | |
struct | lxw_filter_rule |
Options for autofilter rules. More... | |
struct | lxw_image_options |
Options for inserted images. More... | |
struct | lxw_chart_options |
Options for inserted charts. More... | |
struct | lxw_comment_options |
Options for inserted comments. More... | |
struct | lxw_button_options |
Options for inserted buttons. More... | |
struct | lxw_header_footer_options |
Header and footer options. More... | |
struct | lxw_protection |
Worksheet protection options. More... | |
struct | lxw_rich_string_tuple |
Struct to represent a rich string format/string pair. More... | |
struct | lxw_worksheet |
Struct to represent an Excel worksheet. More... | |
Typedefs | |
typedef struct lxw_row_col_options | lxw_row_col_options |
Options for rows and columns. | |
typedef struct lxw_data_validation | lxw_data_validation |
Worksheet data validation options. | |
typedef struct lxw_conditional_format | lxw_conditional_format |
Worksheet conditional formatting options. | |
typedef struct lxw_table_column | lxw_table_column |
Table columns options. | |
typedef struct lxw_table_options | lxw_table_options |
Worksheet table options. | |
typedef struct lxw_filter_rule | lxw_filter_rule |
Options for autofilter rules. | |
typedef struct lxw_image_options | lxw_image_options |
Options for inserted images. | |
typedef struct lxw_chart_options | lxw_chart_options |
Options for inserted charts. | |
typedef struct lxw_comment_options | lxw_comment_options |
Options for inserted comments. | |
typedef struct lxw_button_options | lxw_button_options |
Options for inserted buttons. | |
typedef struct lxw_header_footer_options | lxw_header_footer_options |
Header and footer options. | |
typedef struct lxw_protection | lxw_protection |
Worksheet protection options. | |
typedef struct lxw_rich_string_tuple | lxw_rich_string_tuple |
Struct to represent a rich string format/string pair. | |
typedef struct lxw_worksheet | lxw_worksheet |
Struct to represent an Excel worksheet. | |
Macros | |
#define | LXW_DEF_COL_WIDTH (double)8.43 |
#define | LXW_DEF_ROW_HEIGHT (double)15.0 |
#define | LXW_DEF_COL_WIDTH_PIXELS 64 |
#define | LXW_DEF_ROW_HEIGHT_PIXELS 20 |