libxlsxwriter
Loading...
Searching...
No Matches
merge_rich_string.c
<< merge_range.c autofilter.c >>

Example of merging cells with a rich string in a worksheet.

/*
* An example of merging cells containing a rich string using libxlsxwriter.
*
* Copyright 2014-2024, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("merge_rich_string.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Configure a format for the merged range. */
lxw_format *merge_format = workbook_add_format(workbook);
/* Configure formats for the rich string. */
lxw_format *red = workbook_add_format(workbook);
lxw_format *blue = workbook_add_format(workbook);
/* Create the fragments for the rich string. */
lxw_rich_string_tuple fragment1 = {.format = NULL, .string = "This is " };
lxw_rich_string_tuple fragment2 = {.format = red, .string = "red" };
lxw_rich_string_tuple fragment3 = {.format = NULL, .string = " and this is "};
lxw_rich_string_tuple fragment4 = {.format = blue, .string = "blue" };
lxw_rich_string_tuple *rich_string[] = {&fragment1, &fragment2,
&fragment3, &fragment4, NULL};
/* Write an empty string to the merged range. */
worksheet_merge_range(worksheet, 1, 1, 4, 3, "", merge_format);
/* We then overwrite the first merged cell with a rich string. Note that
* we must also pass the cell format used in the merged cells format at
* the end. */
worksheet_write_rich_string(worksheet, 1, 1, rich_string, merge_format);
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_font_color(lxw_format *format, lxw_color_t color)
Set the color of the font used in the cell.
void format_set_border(lxw_format *format, uint8_t style)
Set the cell border style.
@ LXW_COLOR_BLUE
Definition: format.h:185
@ LXW_COLOR_RED
Definition: format.h:218
@ 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 a rich string format/string pair.
Definition: worksheet.h:2098
lxw_format * format
Definition: worksheet.h:2102
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.
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_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.