@brief Set the properties for one or more columns of cells.
@param worksheet Pointer to a lxw_worksheet instance to be updated.
@param first_col The zero indexed first column.
@param last_col The zero indexed last column.
@param width The width of the column(s).
@param 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:
@code
// Width of columns B:D set to 30.
worksheet_set_column(worksheet, 1, 3, 30, NULL);
@endcode
If %worksheet_set_column() is applied to a single column the value of
first_col and last_col should be the same:
@code
// Width of column B set to 30.
worksheet_set_column(worksheet, 1, 1, 30, NULL);
@endcode
It is also possible, and generally clearer, to specify a column range using
the form of COLS() macro:
// Same as the examples above but clearer.
worksheet_set_column(worksheet, COLS("E:E"), 20, NULL);
worksheet_set_column(worksheet, COLS("F:H"), 30, NULL);
@endcode
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.
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 @ref 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:
@brief Set the properties for one or more columns of cells.
@param worksheet Pointer to a lxw_worksheet instance to be updated. @param first_col The zero indexed first column. @param last_col The zero indexed last column. @param width The width of the column(s). @param 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:
@code // Width of columns B:D set to 30. worksheet_set_column(worksheet, 1, 3, 30, NULL);
@endcode
If %worksheet_set_column() is applied to a single column the value of first_col and last_col should be the same:
@code // Width of column B set to 30. worksheet_set_column(worksheet, 1, 1, 30, NULL);
@endcode
It is also possible, and generally clearer, to specify a column range using the form of COLS() macro:
@code worksheet_set_column(worksheet, 4, 4, 20, NULL); worksheet_set_column(worksheet, 5, 8, 30, NULL);
// Same as the examples above but clearer. worksheet_set_column(worksheet, COLS("E:E"), 20, NULL); worksheet_set_column(worksheet, COLS("F:H"), 30, NULL);
@endcode
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.
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 @ref 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:
@code lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold);
// Set the first column to bold. worksheet_set_column(worksheet, 0, 0, LXW_DEF_COL_HEIGHT, bold); @endcode
The format parameter will be applied to any cells in the column that don't have a format. For example:
@code // Column 1 has format1. worksheet_set_column(worksheet, COLS("A:A"), 8.43, format1);
// Cell A1 in column 1 defaults to format1. worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
// Cell A2 in column 1 keeps format2. worksheet_write_string(worksheet, 1, 0, "Hello", format2); @endcode
As in Excel a row format takes precedence over a default column format:
@code // Row 1 has format1. worksheet_set_row(worksheet, 0, 15, format1);
// Col 1 has format2. worksheet_set_column(worksheet, COLS("A:A"), 8.43, format2);
// Cell A1 defaults to format1, the row format. worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
// Cell A2 keeps format2, the column format. worksheet_write_string(worksheet, 1, 0, "Hello", NULL); @endcode