libxlsxwriter
|
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.
For a general introduction to this Excel feature see An Overview of Excel Tables in the Microsoft Office documentation.
Tables are added to a worksheet using the worksheet_add_table()
function:
Or more explicitly using the RANGE() macro:
The worksheet_add_table()
options
parameter should be a pointer to a lxw_table_options struct with the parameters that describe the table options:
These options are explained in the sections below. There are no required parameters and the options
parameter is itself optional, in which case you can specify NULL and get the default table parameters.
You should take care not to overlap worksheet tables as this is not allowed by Excel and will cause an error when the file is loaded.
constant_memory
mode in workbook_new_opt()
.The no_header_row
parameter can be used to turn off the header row in the table. It is on by default:
Without this option the header row will contain default captions such as Column 1
, Column 2
, etc. These captions can be overridden using the columns
parameter shown below.
The no_autofilter
parameter can be used to turn off the autofilter in the header row. It is on by default:
The autofilter is only shown if the no_header_row
parameter is off (the default). Filter conditions within the table are not supported.
The no_banded_rows
parameter can be used to turn off the rows of alternating color in the table. It is on by default:
The banded_columns
parameter can be used to used to create columns of alternating color in the table. It is off by default:
The banded columns formatting is shown in the image in the previous section above.
The first_column
parameter can be used to highlight the first column of the table. The type of highlighting will depend on the style_type
of the table. It may be bold text or a different color. It is off by default:
The last_column
parameter can be used to highlight the last column of the table. The type of highlighting will depend on the style
of the table. It may be bold text or a different color. It is off by default:
The last_column
formatting is shown in the image in the previous section above.
The style_type
parameter can be used to set the style of the table, in conjunction with the style_type_number
parameter:
There are three types of table style in Excel: Light, Medium and Dark which are represented using the lxw_table_style_type enum values:
Within those ranges there are between 11 and 28 other style types which can be set with style_type_number
(depending on the style type). Check Excel to find the style that you want. The dialog with the options laid out in numeric order are shown below:
The default table style in Excel is 'Table Style Medium 9' (highlighted with a green border in the image above), which is set by default in libxlsxwriter as:
You can also turn the table style off by setting it to Light 0:
The name
parameter is used to set the name of the table. This parameter is optional and by default tables are named Table1
, Table2
, etc. in the worksheet order that they are added.
If you override the table name you must ensure that it doesn't clash with an existing table name and that it follows Excel's requirements for table names, see the Microsoft Office documentation on Naming an Excel Table.
The total_row
parameter can be used to turn on the total row in the last row of a table. It is distinguished from the other rows by a different formatting and also with dropdown SUBTOTAL
functions:
The default total row doesn't have any captions or functions. These must by specified via the columns
parameter below.
The columns
parameter can be used to set properties for columns within the table.
The sub-properties of lxw_table_column
that can be set are:
lxw_table_column.header
lxw_table_column.header_format
lxw_table_column.formula
lxw_table_column.total_string
lxw_table_column.total_function
lxw_table_column.total_value
lxw_table_column.format
The columns
parameter should be a NULL terminated array of lxw_table_column
pointers. For example to override the default 'Column n' style table headers:
If you don't wish to specify properties for a specific column you can pass an empty (but not NULL) struct and the defaults will be applied:
Column formulas can by applied using the column formula
property:
The Excel 2007 style "#This Row"
and Excel 2010 style "@"
structural references are supported within the formula. However, other Excel 2010 additions to structural references aren't supported and formulas should conform to Excel 2007 style formulas. See the Microsoft documentation on [Using structured references with Excel tables] (http://office.microsoft.com/en-us/excel-help/using-structured-references-with-excel-tables-HA010155686.aspx) for details.
As stated above the total_row
table parameter turns on the "Total" row in the table but it doesn't populate it with any defaults. Total captions and functions must be specified via the columns
property and the total_string
and total_function
sub properties:
The supported totals row SUBTOTAL
functions are defined in lxw_table_total_functions:
User defined functions or formulas aren't supported.
It is also possible to set a calculated value for the total_function
using the total_value
sub property. This is only necessary when creating workbooks for applications that cannot calculate the value of formulas automatically. This is similar to setting the result
property in worksheet_write_formula_num()
. See also Formula Results.
Formatting can also be applied to columns using the format
and to the header using header_format
(although you will also need to add it to the data in the column, see the next section):
Standard libxlsxwriter lxw_format objects are used for this formatting. However, they should be limited to numerical formats for the columns and simple formatting like text wrap for the headers. Overriding other table formatting may produce inconsistent results. You will also need to apply the same format to any data you write to the column in the table, see the next section.
Once you create a worksheet table you will also need to fill in the data in the rows and columns. This is done with the standard worksheet write() functions. For example the data in the examples above was written as follows:
All of the images shown above are taken from tables.c.