libxlsxwriter
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-2018, 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;
}
workbook_close
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
lxw_rich_string_tuple
Struct to represent a rich string format/string pair.
Definition: worksheet.h:1586
LXW_ALIGN_VERTICAL_CENTER
@ LXW_ALIGN_VERTICAL_CENTER
Definition: format.h:153
format_set_align
void format_set_align(lxw_format *format, uint8_t alignment)
Set the alignment for data in the cell.
workbook_new
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
format_set_border
void format_set_border(lxw_format *format, uint8_t style)
Set the cell border style.
lxw_rich_string_tuple::format
lxw_format * format
Definition: worksheet.h:1590
lxw_worksheet
Struct to represent an Excel worksheet.
Definition: worksheet.h:1603
lxw_format
Struct to represent the formatting properties of an Excel format.
Definition: format.h:358
LXW_ALIGN_CENTER
@ LXW_ALIGN_CENTER
Definition: format.h:129
lxw_workbook
Struct to represent an Excel workbook.
Definition: workbook.h:273
LXW_COLOR_BLUE
@ LXW_COLOR_BLUE
Definition: format.h:184
LXW_COLOR_RED
@ LXW_COLOR_RED
Definition: format.h:217
format_set_font_color
void format_set_font_color(lxw_format *format, lxw_color_t color)
Set the color of the font used in the cell.
worksheet_write_rich_string
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_BORDER_THIN
@ LXW_BORDER_THIN
Definition: format.h:295
workbook_add_worksheet
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
workbook_add_format
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
worksheet_merge_range
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.