libxlsxwriter
rich_strings.c
<< hyperlinks.c array_formula.c >>

Example of writing "rich" multi-format strings to a worksheet.

/*
* An example of using the libxlsxwriter library to write some "rich strings",
* i.e., strings with multiple formats.
*
* Copyright 2014-2018, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("rich_strings.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Set up some formats to use. */
lxw_format *bold = workbook_add_format(workbook);
lxw_format *italic = workbook_add_format(workbook);
lxw_format *red = workbook_add_format(workbook);
lxw_format *blue = workbook_add_format(workbook);
lxw_format *center = workbook_add_format(workbook);
lxw_format *superscript = workbook_add_format(workbook);
/* Make the first column wider for clarity. */
worksheet_set_column(worksheet, 0, 0, 30, NULL);
/*
* Create and write some rich strings with multiple formats.
*/
/* Example 1. Some bold and italic in the same string. */
lxw_rich_string_tuple fragment11 = {.format = NULL, .string = "This is " };
lxw_rich_string_tuple fragment12 = {.format = bold, .string = "bold" };
lxw_rich_string_tuple fragment13 = {.format = NULL, .string = " and this is "};
lxw_rich_string_tuple fragment14 = {.format = italic, .string = "italic" };
lxw_rich_string_tuple *rich_string1[] = {&fragment11, &fragment12,
&fragment13, &fragment14, NULL};
worksheet_write_rich_string(worksheet, CELL("A1"), rich_string1, NULL);
/* Example 2. Some red and blue coloring in the same string. */
lxw_rich_string_tuple fragment21 = {.format = NULL, .string = "This is " };
lxw_rich_string_tuple fragment22 = {.format = red, .string = "red" };
lxw_rich_string_tuple fragment23 = {.format = NULL, .string = " and this is "};
lxw_rich_string_tuple fragment24 = {.format = blue, .string = "blue" };
lxw_rich_string_tuple *rich_string2[] = {&fragment21, &fragment22,
&fragment23, &fragment24, NULL};
worksheet_write_rich_string(worksheet, CELL("A3"), rich_string2, NULL);
/* Example 3. A rich string plus cell formatting. */
lxw_rich_string_tuple fragment31 = {.format = NULL, .string = "Some " };
lxw_rich_string_tuple fragment32 = {.format = bold, .string = "bold text"};
lxw_rich_string_tuple fragment33 = {.format = NULL, .string = " centered"};
lxw_rich_string_tuple *rich_string3[] = {&fragment31, &fragment32,
&fragment33, NULL};
/* Note that this example also has a "center" cell format. */
worksheet_write_rich_string(worksheet, CELL("A5"), rich_string3, center);
/* Example 4. A math example with a superscript. */
lxw_rich_string_tuple fragment41 = {.format = italic, .string = "j =k" };
lxw_rich_string_tuple fragment42 = {.format = superscript, .string = "(n-1)"};
lxw_rich_string_tuple *rich_string4[] = {&fragment41, &fragment42, NULL};
worksheet_write_rich_string(worksheet, CELL("A7"), rich_string4, center);
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
format_set_font_script
void format_set_font_script(lxw_format *format, uint8_t style)
Set the superscript/subscript property of the font.
format_set_italic
void format_set_italic(lxw_format *format)
Turn on italic for the format font.
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.
LXW_FONT_SUPERSCRIPT
@ LXW_FONT_SUPERSCRIPT
Definition: format.h:114
lxw_rich_string_tuple::format
lxw_format * format
Definition: worksheet.h:1590
format_set_bold
void format_set_bold(lxw_format *format)
Turn on bold for the format font.
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.
worksheet_set_column
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.
CELL
#define CELL(cell)
Convert an Excel A1 cell string into a (row, col) pair.
Definition: utility.h:45
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.