libxlsxwriter
Loading...
Searching...
No Matches
panes.c
<< hide_row_col.c ignore_errors.c >>

An example of how to create panes in a worksheet, both "freeze" panes and "split" panes.

/*
* A simple example using the libxlsxwriter library to create worksheets with
* panes.
*
* Copyright 2014-2024, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
int row;
int col;
/* Create a new workbook and add some worksheets. */
lxw_workbook *workbook = workbook_new("panes.xlsx");
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Panes 1");
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Panes 2");
lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, "Panes 3");
lxw_worksheet *worksheet4 = workbook_add_worksheet(workbook, "Panes 4");
/* Set up some formatting and text to highlight the panes. */
lxw_format *header = workbook_add_format(workbook);
format_set_fg_color(header, 0xD7E4BC);
format_set_bold(header);
lxw_format *center = workbook_add_format(workbook);
/*
* Example 1. Freeze pane on the top row.
*/
worksheet_freeze_panes(worksheet1, 1, 0);
/* Some sheet formatting. */
worksheet_set_column(worksheet1, 0, 8, 16, NULL);
worksheet_set_row(worksheet1, 0, 20, NULL);
worksheet_set_selection(worksheet1, 4, 3, 4, 3);
/* Some worksheet text to demonstrate scrolling. */
for (col = 0; col < 9; col++) {
worksheet_write_string(worksheet1, 0, col, "Scroll down", header);
}
for (row = 1; row < 100; row++) {
for (col = 0; col < 9; col++) {
worksheet_write_number(worksheet1, row, col, row + 1, center);
}
}
/*
* Example 2. Freeze pane on the left column.
*/
worksheet_freeze_panes(worksheet2, 0, 1);
/* Some sheet formatting. */
worksheet_set_column(worksheet2, 0, 0, 16, NULL);
worksheet_set_selection(worksheet2, 4, 3, 4, 3);
/* Some worksheet text to demonstrate scrolling. */
for (row = 0; row < 50; row++) {
worksheet_write_string(worksheet2, row, 0, "Scroll right", header);
for (col = 1; col < 26; col++) {
worksheet_write_number(worksheet2, row, col, col, center);
}
}
/*
* Example 3. Freeze pane on the top row and left column.
*/
worksheet_freeze_panes(worksheet3, 1, 1);
/* Some sheet formatting. */
worksheet_set_column(worksheet3, 0, 25, 16, NULL);
worksheet_set_row(worksheet3, 0, 20, NULL);
worksheet_write_string(worksheet3, 0, 0, "", header);
worksheet_set_selection(worksheet3, 4, 3, 4, 3);
/* Some worksheet text to demonstrate scrolling. */
for (col = 1; col < 26; col++) {
worksheet_write_string(worksheet3, 0, col, "Scroll down", header);
}
for (row = 1; row < 50; row++) {
worksheet_write_string(worksheet3, row, 0, "Scroll right", header);
for (col = 1; col < 26; col++) {
worksheet_write_number(worksheet3, row, col, col, center);
}
}
/*
* Example 4. Split pane on the top row and left column.
*
* The divisions must be specified in terms of row and column dimensions.
* The default row height is 15 and the default column width is 8.43
*/
worksheet_split_panes(worksheet4, 15, 8.43);
/* Some sheet formatting. */
/* Some worksheet text to demonstrate scrolling. */
for (col = 1; col < 26; col++) {
worksheet_write_string(worksheet4, 0, col, "Scroll", center);
}
for (row = 1; row < 50; row++) {
worksheet_write_string(worksheet4, row, 0, "Scroll", center);
for (col = 1; col < 26; col++) {
worksheet_write_number(worksheet4, row, col, col, center);
}
}
workbook_close(workbook);
return 0;
}
void format_set_align(lxw_format *format, uint8_t alignment)
Set the alignment for data in the cell.
@ LXW_BORDER_THIN
Definition: format.h:296
void format_set_fg_color(lxw_format *format, lxw_color_t color)
Set the pattern foreground color for a cell.
void format_set_bold(lxw_format *format)
Turn on bold for the format font.
void format_set_border(lxw_format *format, uint8_t style)
Set the cell border style.
@ LXW_ALIGN_CENTER
Definition: format.h:130
@ LXW_ALIGN_VERTICAL_CENTER
Definition: format.h:154
Struct to represent the formatting properties of an Excel format.
Definition: format.h:359
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2115
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
void worksheet_freeze_panes(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col)
Split and freeze a worksheet into panes.
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.
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:
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_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_number(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, double number, lxw_format *format)
Write a number to a worksheet cell.