int main() { lxw_workbook *workbook = new_workbook("test_format02.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *format1 = workbook_add_format(workbook); lxw_format *format2 = workbook_add_format(workbook); worksheet_set_row(worksheet, 0, 30, NULL, NULL); format_set_font_name(format1, "Arial"); format_set_bold(format1); format_set_align(format1, LXW_ALIGN_LEFT); format_set_align(format1, LXW_ALIGN_VERTICAL_BOTTOM); format_set_font_name(format2, "Arial"); format_set_bold(format2); format_set_rotation(format2, 90); format_set_align(format2, LXW_ALIGN_CENTER); format_set_align(format2, LXW_ALIGN_VERTICAL_BOTTOM); worksheet_write_string(worksheet, 0, 0, "Foo", format1); worksheet_write_string(worksheet, 0, 1, "Bar", format2); return workbook_close(workbook); }
// Test the _write_font() function. CTEST(styles, write_font13) { char* got; char exp[] = "<font><b/><i/><strike/><outline/><shadow/><u/><vertAlign val=\"superscript\"/><sz val=\"12\"/><color rgb=\"FFFF0000\"/><name val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></font>"; FILE* testfile = lxw_tmpfile(NULL); lxw_styles *styles = lxw_styles_new(); lxw_format *format = lxw_format_new(); format_set_bold(format); format_set_italic(format); format_set_font_size(format, 12); format_set_font_color(format, LXW_COLOR_RED); format_set_font_strikeout(format); format_set_font_outline(format); format_set_font_shadow(format); format_set_font_script(format, LXW_FONT_SUPERSCRIPT); format_set_underline(format, LXW_UNDERLINE_SINGLE); styles->file = testfile; _write_font(styles, format, LXW_FALSE); RUN_XLSX_STREQ(exp, got); lxw_styles_free(styles); lxw_format_free(format); }
int main() { lxw_workbook *workbook = new_workbook("test_outline01.xlsx"); lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Outlined Rows"); lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); lxw_row_col_options options1 = {.hidden = 0, .level = 2, .collapsed = 0}; lxw_row_col_options options2 = {.hidden = 0, .level = 1, .collapsed = 0}; worksheet_set_column(worksheet1, COLS("A:A"), 20, NULL); worksheet_set_row_opt(worksheet1, 1, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 2, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 3, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 4, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 5, LXW_DEF_ROW_HEIGHT, NULL, &options2); worksheet_set_row_opt(worksheet1, 6, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 7, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 8, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 9, LXW_DEF_ROW_HEIGHT, NULL, &options1); worksheet_set_row_opt(worksheet1, 10, LXW_DEF_ROW_HEIGHT, NULL, &options2); worksheet_write_string(worksheet1, CELL("A1"), "Region", bold); worksheet_write_string(worksheet1, CELL("A2"), "North" , NULL); worksheet_write_string(worksheet1, CELL("A3"), "North" , NULL); worksheet_write_string(worksheet1, CELL("A4"), "North" , NULL); worksheet_write_string(worksheet1, CELL("A5"), "North" , NULL); worksheet_write_string(worksheet1, CELL("A6"), "North Total", bold); worksheet_write_string(worksheet1, CELL("B1"), "Sales", bold); worksheet_write_number(worksheet1, CELL("B2"), 1000 , NULL); worksheet_write_number(worksheet1, CELL("B3"), 1200 , NULL); worksheet_write_number(worksheet1, CELL("B4"), 900 , NULL); worksheet_write_number(worksheet1, CELL("B5"), 1200 , NULL); worksheet_write_formula_num(worksheet1, CELL("B6"), "=SUBTOTAL(9,B2:B5)", bold, 4300); worksheet_write_string(worksheet1, CELL("A7"), "South" , NULL); worksheet_write_string(worksheet1, CELL("A8"), "South" , NULL); worksheet_write_string(worksheet1, CELL("A9"), "South" , NULL); worksheet_write_string(worksheet1, CELL("A10"), "South" , NULL); worksheet_write_string(worksheet1, CELL("A11"), "South Total", bold); worksheet_write_number(worksheet1, CELL("B7"), 400 , NULL); worksheet_write_number(worksheet1, CELL("B8"), 600 , NULL); worksheet_write_number(worksheet1, CELL("B9"), 500 , NULL); worksheet_write_number(worksheet1, CELL("B10"), 600 , NULL); worksheet_write_formula_num(worksheet1, CELL("B11"), "=SUBTOTAL(9,B7:B10)", bold, 2100); worksheet_write_string(worksheet1, CELL("A12"), "Grand Total", bold); worksheet_write_formula_num(worksheet1, CELL("B12"), "=SUBTOTAL(9,B2:B10)", bold, 6400); return workbook_close(workbook); }
int main() { /* Create a new workbook. */ lxw_workbook *workbook = workbook_new("anatomy.xlsx"); /* Add a worksheet with a user defined sheet name. */ lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Demo"); /* Add a worksheet with Excel's default sheet name: Sheet2. */ lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, NULL); /* Add some cell formats. */ lxw_format *myformat1 = workbook_add_format(workbook); lxw_format *myformat2 = workbook_add_format(workbook); /* Set the bold property for the first format. */ format_set_bold(myformat1); /* Set a number format for the second format. */ format_set_num_format(myformat2, "$#,##0.00"); /* Widen the first column to make the text clearer. */ worksheet_set_column(worksheet1, 0, 0, 20, NULL); /* Write some unformatted data. */ worksheet_write_string(worksheet1, 0, 0, "Peach", NULL); worksheet_write_string(worksheet1, 1, 0, "Plum", NULL); /* Write formatted data. */ worksheet_write_string(worksheet1, 2, 0, "Pear", myformat1); /* Formats can be reused. */ worksheet_write_string(worksheet1, 3, 0, "Persimmon", myformat1); /* Write some numbers. */ worksheet_write_number(worksheet1, 5, 0, 123, NULL); worksheet_write_number(worksheet1, 6, 0, 4567.555, myformat2); /* Write to the second worksheet. */ worksheet_write_string(worksheet2, 0, 0, "Some text", myformat1); /* Close the workbook, save the file and free any memory. */ uint8_t error = workbook_close(workbook); /* Check if there was any error creating the xlsx file. */ if (error) printf("Error in workbook_close().\n" "Error %d = %s\n", error, lxw_strerror(error)); return error; }
int main() { lxw_workbook *workbook = new_workbook("test_row_col_format07.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); worksheet_set_row(worksheet, 4, 15, bold, NULL); return workbook_close(workbook); }
int main() { lxw_workbook *workbook = new_workbook("test_row_col_format10.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); worksheet_set_column(worksheet, 2, 2, LXW_DEF_COL_WIDTH, bold, NULL); return workbook_close(workbook); }
int main() { lxw_workbook *workbook = workbook_new("test_row_col_format18.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); worksheet_set_row(worksheet, 1048575, 15, bold); worksheet_write_string(worksheet, 1048575, 0, "Bar", NULL); return workbook_close(workbook); }
int main() { lxw_workbook_options options = {1, NULL}; lxw_workbook *workbook = workbook_new_opt("test_optimize23.xlsx", &options); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); worksheet_set_row(worksheet, 0, 20, bold); return workbook_close(workbook); }
/* * Create a worksheet with examples charts. */ int main() { lxw_workbook *workbook = new_workbook("chartsheet.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chartsheet *chartsheet = workbook_add_chartsheet(workbook, NULL); lxw_chart_series *series; /* Add a bold format to use to highlight the header cells. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); /* Write some data for the chart. */ write_worksheet_data(worksheet, bold); /* Create a bar chart. */ lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); /* Set the name for the series instead of the default "Series 1". */ chart_series_set_name(series, "=Sheet1!$B$1"); /* Add a second series but leave the categories and values undefined. They * can be defined later using the alternative syntax shown below. */ series = chart_add_series(chart, NULL, NULL); /* Configure the series using a syntax that is easier to define programmatically. */ chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */ chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */ chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */ /* Add a chart title and some axis labels. */ chart_title_set_name(chart, "Results of sample analysis"); chart_axis_set_name(chart->x_axis, "Test number"); chart_axis_set_name(chart->y_axis, "Sample length (mm)"); /* Set an Excel chart style. */ chart_set_style(chart, 11); /* Add the chart to the chartsheet. */ chartsheet_set_chart(chartsheet, chart); /* Display the chartsheet as the active sheet when the workbook is opened. */ chartsheet_activate(chartsheet); return workbook_close(workbook); }
int main() { lxw_workbook *workbook = workbook_new("test_row_col_format05.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); lxw_format *italic = workbook_add_format(workbook); format_set_italic(italic); worksheet_set_column(worksheet, 0, 0, 8.43, bold); worksheet_set_column(worksheet, 2, 2, 8.43, italic); return workbook_close(workbook); }
int main() { lxw_workbook *workbook = new_workbook("test_simple02.xlsx"); lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, NULL); lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Data Sheet"); lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, NULL); lxw_format *format = workbook_add_format(workbook); format_set_bold(format); worksheet_write_string(worksheet1, 0, 0, "Foo", NULL); worksheet_write_number(worksheet1, 1, 0, 123, NULL); worksheet_write_string(worksheet3, 1, 1, "Foo", NULL); worksheet_write_string(worksheet3, 2, 1, "Bar", format); worksheet_write_number(worksheet3, 3, 2, 234, NULL); (void)worksheet2; /* Unused. For testing only. */ return workbook_close(workbook); }
// Test the _write_font() function. CTEST(styles, write_font02) { char* got; char exp[] = "<font><b/><sz val=\"11\"/><color theme=\"1\"/><name val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></font>"; FILE* testfile = lxw_tmpfile(NULL); lxw_styles *styles = lxw_styles_new(); lxw_format *format = lxw_format_new(); format_set_bold(format); styles->file = testfile; _write_font(styles, format, LXW_FALSE); RUN_XLSX_STREQ(exp, got); lxw_styles_free(styles); lxw_format_free(format); }
int main() { lxw_workbook *workbook = new_workbook("test_rich_string04.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *bold = workbook_add_format(workbook); lxw_format *italic = workbook_add_format(workbook); format_set_bold(bold); format_set_italic(italic); worksheet_write_string(worksheet, CELL("A1"), "Foo", bold); worksheet_write_string(worksheet, CELL("A2"), "Bar", italic); lxw_rich_string_tuple fragment1 = {.format = bold, .string = "abc"}; lxw_rich_string_tuple fragment2 = {.format = italic, .string = "defg"}; lxw_rich_string_tuple *rich_strings[] = {&fragment1, &fragment2, NULL}; worksheet_write_rich_string(worksheet, CELL("A3"), rich_strings, NULL); return workbook_close(workbook); }
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_align(header, LXW_ALIGN_CENTER); format_set_align(header, LXW_ALIGN_VERTICAL_CENTER); format_set_fg_color(header, 0xD7E4BC); format_set_bold(header); format_set_border(header, LXW_BORDER_THIN); lxw_format *center = workbook_add_format(workbook); format_set_align(center, LXW_ALIGN_CENTER); /* * 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; }
/* * Create a worksheet with examples charts. * */ int main() { lxw_workbook *workbook = new_workbook("chart_pie.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart *chart ; lxw_chart_series *series; /* Add a bold format to use to highlight the header cells. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); /* Write some data for the chart. */ write_worksheet_data(worksheet, bold); /* * Chart 1: Create a simple pie chart. */ chart = workbook_add_chart(workbook, LXW_CHART_PIE); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4"); /* Set the name for the series instead of the default "Series 1". */ chart_series_set_name(series, "Pie sales data"); /* Add a chart title. */ chart_title_set_name(chart, "Popular Pie Types"); /* Set an Excel chart style. */ chart_set_style(chart, 10); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("D2"), chart); /* * Chart 2: Create a pie chart with user defined segment colors. */ chart = workbook_add_chart(workbook, LXW_CHART_PIE); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4"); /* Set the name for the series instead of the default "Series 1". */ chart_series_set_name(series, "Pie sales data"); /* Add a chart title. */ chart_title_set_name(chart, "Pie Chart with user defined colors"); /* Add for fills for use in the chart. */ lxw_chart_fill fill1 = {.color = 0x5ABA10}; lxw_chart_fill fill2 = {.color = 0xFE110E}; lxw_chart_fill fill3 = {.color = 0xCA5C05}; /* Add some points with the above fills. */ lxw_chart_point point1 = {.fill = &fill1}; lxw_chart_point point2 = {.fill = &fill2}; lxw_chart_point point3 = {.fill = &fill3}; /* Create an array of the point objects. */ lxw_chart_point *points[] = {&point1, &point2, &point3, NULL}; /* Add/override the points/segments of the chart. */ chart_series_set_points(series, points); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("D18"), chart); /* * Chart 3: Create a pie chart with rotation of the segments. */ chart = workbook_add_chart(workbook, LXW_CHART_PIE); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4"); /* Set the name for the series instead of the default "Series 1". */ chart_series_set_name(series, "Pie sales data"); /* Add a chart title. */ chart_title_set_name(chart, "Pie Chart with segment rotation"); /* Change the angle/rotation of the first segment. */ chart_set_rotation(chart, 90); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("D34"), chart); return workbook_close(workbook); }
/* * Create a worksheet with examples charts. */ int main() { lxw_workbook *workbook = new_workbook("chart_data_tools.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart_series *series; /* Add a bold format to use to highlight the header cells. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); /* Write some data for the chart. */ write_worksheet_data(worksheet, bold); /* * Chart 1. Example with High Low Lines. */ lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with High-Low Lines"); /* Add the first series to the chart. */ chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add high-low lines to the chart. */ chart_set_high_low_lines(chart, NULL); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E2"), chart); /* * Chart 2. Example with Drop Lines. */ chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with Drop Lines"); /* Add the first series to the chart. */ chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add drop lines to the chart. */ chart_set_drop_lines(chart, NULL); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E18"), chart); /* * Chart 3. Example with Up-Down bars. */ chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with Up-Down bars"); /* Add the first series to the chart. */ chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add Up-Down bars to the chart. */ chart_set_up_down_bars(chart); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E34"), chart); /* * Chart 4. Example with Up-Down bars with formatting. */ chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with Up-Down bars"); /* Add the first series to the chart. */ chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add Up-Down bars to the chart, with formatting. */ lxw_chart_line line = {.color = LXW_COLOR_BLACK}; lxw_chart_fill up_fill = {.color = 0x00B050}; lxw_chart_fill down_fill = {.color = LXW_COLOR_RED}; chart_set_up_down_bars_format(chart, &line, &up_fill, &line, &down_fill); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E50"), chart); /* * Chart 5. Example with Markers and data labels. */ chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with Data Labels and Markers"); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add series markers. */ chart_series_set_marker_type(series, LXW_CHART_MARKER_CIRCLE); /* Add series data labels. */ chart_series_set_labels(series); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E66"), chart); /* * Chart 6. Example with Error Bars. */ chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with Error Bars"); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add error bars to show Standard Error. */ chart_series_set_error_bars(series->y_error_bars, LXW_CHART_ERROR_BAR_TYPE_STD_ERROR, 0); /* Add series data labels. */ chart_series_set_labels(series); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E82"), chart); /* * Chart 7. Example with a trendline */ chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add a chart title. */ chart_title_set_name(chart, "Chart with a Trendline"); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Add a polynomial trendline. */ lxw_chart_line poly_line = {.color = LXW_COLOR_GRAY, .dash_type = LXW_CHART_LINE_DASH_LONG_DASH}; chart_series_set_trendline(series, LXW_CHART_TRENDLINE_TYPE_POLY, 3); chart_series_set_trendline_line(series, &poly_line); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E98"), chart); return workbook_close(workbook); }
/* Create a worksheet with a chart. */ int main() { lxw_workbook *workbook = new_workbook("chart_pattern.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart *chart; /* Add a bold format to use to highlight the header cells. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); /* Write some data for the chart. */ worksheet_write_string(worksheet, 0, 0, "Shingle", bold); worksheet_write_number(worksheet, 1, 0, 105, NULL); worksheet_write_number(worksheet, 2, 0, 150, NULL); worksheet_write_number(worksheet, 3, 0, 130, NULL); worksheet_write_number(worksheet, 4, 0, 90, NULL); worksheet_write_string(worksheet, 0, 1, "Brick", bold); worksheet_write_number(worksheet, 1, 1, 50, NULL); worksheet_write_number(worksheet, 2, 1, 120, NULL); worksheet_write_number(worksheet, 3, 1, 100, NULL); worksheet_write_number(worksheet, 4, 1, 110, NULL); /* Create a chart object. */ chart = workbook_add_chart(workbook, LXW_CHART_COLUMN); /* Configure the chart. */ lxw_chart_series *series1 = chart_add_series(chart, NULL, "Sheet1!$A$2:$A$5"); lxw_chart_series *series2 = chart_add_series(chart, NULL, "Sheet1!$B$2:$B$5"); chart_series_set_name(series1, "=Sheet1!$A$1"); chart_series_set_name(series2, "=Sheet1!$B$1"); chart_title_set_name(chart, "Cladding types"); chart_axis_set_name(chart->x_axis, "Region"); chart_axis_set_name(chart->y_axis, "Number of houses"); /* Configure an add the chart series patterns. */ lxw_chart_pattern pattern1 = {.type = LXW_CHART_PATTERN_SHINGLE, .fg_color = 0x804000, .bg_color = 0XC68C53}; lxw_chart_pattern pattern2 = {.type = LXW_CHART_PATTERN_HORIZONTAL_BRICK, .fg_color = 0XB30000, .bg_color = 0XFF6666}; chart_series_set_pattern(series1, &pattern1); chart_series_set_pattern(series2, &pattern2); /* Configure and set the chart series borders. */ lxw_chart_line line1 = {.color = 0x804000}; lxw_chart_line line2 = {.color = 0xb30000}; chart_series_set_line(series1, &line1); chart_series_set_line(series2, &line2); /* Widen the gap between the series/categories. */ chart_set_series_gap(chart, 70); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("D2"), chart); return workbook_close(workbook); }