A more comprehensive example of how to add conditional formatting to an libxlsxwriter file. Conditional formatting allows you to apply a format to a cell or a range of cells based on certain criteria.
#include "xlsxwriter.h"
uint8_t data[10][10] = {
{34, 72, 38, 30, 75, 48, 75, 66, 84, 86},
{6, 24, 1, 84, 54, 62, 60, 3, 26, 59},
{28, 79, 97, 13, 85, 93, 93, 22, 5, 14},
{27, 71, 40, 17, 18, 79, 90, 93, 29, 47},
{88, 25, 33, 23, 67, 1, 59, 79, 47, 36},
{24, 100, 20, 88, 29, 33, 38, 54, 54, 88},
{6, 57, 88, 28, 10, 26, 37, 7, 41, 48},
{52, 78, 1, 96, 26, 45, 47, 33, 96, 36},
{60, 54, 81, 66, 81, 90, 80, 93, 12, 55},
{70, 5, 46, 14, 71, 19, 66, 36, 41, 21},
};
int row, col;
for (row = 0; row < 10; row++)
for (col = 0; col < 10; col++)
}
}
int main() {
write_worksheet_data(worksheet1);
"Cells with values >= 50 are in light red. "
"Values < 50 are in light green.",
NULL);
conditional_format->
value = 50;
conditional_format->
format = format1;
conditional_format->
value = 50;
conditional_format->
format = format2;
write_worksheet_data(worksheet2);
"Values between 30 and 70 are in light red. "
"Values outside that range are in light green.",
NULL);
conditional_format->
format = format1;
conditional_format->
format = format2;
write_worksheet_data(worksheet3);
"Duplicate values are in light red. "
"Unique values are in light green.",
NULL);
conditional_format->
format = format1;
conditional_format->
format = format2;
write_worksheet_data(worksheet4);
"Above average values are in light red. "
"Below average values are in light green.",
NULL);
conditional_format->
format = format1;
conditional_format->
format = format2;
write_worksheet_data(worksheet5);
"Top 10 values are in light red. "
"Bottom 10 values are in light green.",
NULL);
conditional_format->
value = 10;
conditional_format->
format = format1;
conditional_format->
value = 10;
conditional_format->
format = format2;
write_worksheet_data(worksheet6);
"Cells with values >= 50 are in light red."
"Values < 50 are in light green. Non-contiguous ranges.",
NULL);
conditional_format->
value = 50;
conditional_format->
format = format1;
conditional_format->
value = 50;
conditional_format->
format = format2;
reset_conditional_format(conditional_format);
for (int i = 1; i <= 12; i++) {
}
"Examples of color scales with default and user colors.",
NULL);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
for (int i = 1; i <= 12; i++) {
}
int data[] = {-1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0};
for (int i = 1; i <= 12; i++) {
}
"Examples of data bars.",
NULL);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
for (int i = 1; i <= 3; i++) {
}
for (int i = 1; i <= 4; i++) {
}
for (int i = 1; i <= 5; i++) {
}
"Examples of conditional formats with icon sets.",
NULL);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
reset_conditional_format(conditional_format);
free(conditional_format);
}
@ LXW_TRUE
Definition: common.h:54
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2115
#define RANGE(range)
Convert an Excel A1:B2 range into a (first_row, first_col, last_row, last_col) sequence.
Definition: utility.h:83
#define CELL(cell)
Convert an Excel A1 cell string into a (row, col) pair.
Definition: utility.h:46
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_CONDITIONAL_BAR_DIRECTION_RIGHT_TO_LEFT
Definition: worksheet.h:453
@ LXW_CONDITIONAL_CRITERIA_BETWEEN
Definition: worksheet.h:327
@ LXW_CONDITIONAL_CRITERIA_NOT_BETWEEN
Definition: worksheet.h:330
@ LXW_CONDITIONAL_CRITERIA_AVERAGE_ABOVE
Definition: worksheet.h:375
@ LXW_CONDITIONAL_CRITERIA_GREATER_THAN_OR_EQUAL_TO
Definition: worksheet.h:321
@ LXW_CONDITIONAL_CRITERIA_AVERAGE_BELOW
Definition: worksheet.h:378
@ LXW_CONDITIONAL_CRITERIA_LESS_THAN
Definition: worksheet.h:318
@ LXW_CONDITIONAL_ICONS_4_ARROWS_COLORED
Definition: worksheet.h:510
@ LXW_CONDITIONAL_ICONS_5_ARROWS_COLORED
Definition: worksheet.h:527
@ LXW_CONDITIONAL_ICONS_3_TRAFFIC_LIGHTS_UNRIMMED
Definition: worksheet.h:493
@ LXW_CONDITIONAL_ICONS_3_ARROWS_COLORED
Definition: worksheet.h:484
@ LXW_CONDITIONAL_ICONS_5_RATINGS
Definition: worksheet.h:534
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.
lxw_error worksheet_conditional_format_range(lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, lxw_conditional_format *conditional_format)
Add a conditional format to a worksheet range.
@ LXW_CONDITIONAL_TYPE_UNIQUE
Definition: worksheet.h:256
@ LXW_CONDITIONAL_2_COLOR_SCALE
Definition: worksheet.h:284
@ LXW_CONDITIONAL_TYPE_TOP
Definition: worksheet.h:260
@ LXW_CONDITIONAL_TYPE_DUPLICATE
Definition: worksheet.h:253
@ LXW_CONDITIONAL_TYPE_BOTTOM
Definition: worksheet.h:264
@ LXW_CONDITIONAL_3_COLOR_SCALE
Definition: worksheet.h:288
@ LXW_CONDITIONAL_TYPE_AVERAGE
Definition: worksheet.h:250
@ LXW_CONDITIONAL_TYPE_CELL
Definition: worksheet.h:238
@ LXW_CONDITIONAL_TYPE_ICON_SETS
Definition: worksheet.h:296
@ LXW_CONDITIONAL_DATA_BAR
Definition: worksheet.h:292