Exemplo n.º 1
0
int main() {

    lxw_workbook  *workbook = workbook_new("defined_name.xlsx");
    lxw_worksheet *worksheet;

    /* We don't use the returned worksheets in this example and use a generic
     * loop instead. */
    workbook_add_worksheet(workbook, NULL);
    workbook_add_worksheet(workbook, NULL);

    /* Define some global/workbook names. */
    workbook_define_name(workbook, "Sales", "=!G1:H10");

    workbook_define_name(workbook, "Exchange_rate", "=0.96");
    workbook_define_name(workbook, "Sales",         "=Sheet1!$G$1:$H$10");

    /* Define a local/worksheet name. */
    workbook_define_name(workbook, "Sheet2!Sales",  "=Sheet2!$G$1:$G$10");

    /* Write some text to the worksheets and one of the defined name in a formula. */
    LXW_FOREACH_WORKSHEET(worksheet, workbook){
        worksheet_set_column(worksheet, 0, 0, 45, NULL);

        worksheet_write_string(worksheet, 0, 0,
                               "This worksheet contains some defined names.", NULL);

        worksheet_write_string(worksheet, 1, 0,
                               "See Formulas -> Name Manager above.", NULL);

        worksheet_write_string(worksheet, 2, 0,
                               "Example formula in cell B3 ->", NULL);

        worksheet_write_formula(worksheet, 2, 1, "=Exchange_rate", NULL);
    }
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);
}
Exemplo n.º 3
0
int main() {

    lxw_workbook  *workbook  = new_workbook("test_chart_bar22.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_chart     *chart     = workbook_add_chart(workbook, LXW_CHART_BAR);

    /* For testing, copy the randomly generated axis ids in the target file. */
    chart->axis_id_1 = 43706240;
    chart->axis_id_2 = 43727104;


    worksheet_write_string(worksheet, 0, 1, "Series 1", NULL);
    worksheet_write_string(worksheet, 0, 2, "Series 2", NULL);
    worksheet_write_string(worksheet, 0, 3, "Series 3", NULL);

    worksheet_write_string(worksheet, 1, 0, "Category 1", NULL);
    worksheet_write_string(worksheet, 2, 0, "Category 2", NULL);
    worksheet_write_string(worksheet, 3, 0, "Category 3", NULL);
    worksheet_write_string(worksheet, 4, 0, "Category 4", NULL);

    worksheet_write_number(worksheet, 1, 1, 4.3, NULL);
    worksheet_write_number(worksheet, 2, 1, 2.5, NULL);
    worksheet_write_number(worksheet, 3, 1, 3.5, NULL);
    worksheet_write_number(worksheet, 4, 1, 4.5, NULL);

    worksheet_write_number(worksheet, 1, 2, 2.4, NULL);
    worksheet_write_number(worksheet, 2, 2, 4.5, NULL);
    worksheet_write_number(worksheet, 3, 2, 1.8, NULL);
    worksheet_write_number(worksheet, 4, 2, 2.8, NULL);

    worksheet_write_number(worksheet, 1, 3, 2,   NULL);
    worksheet_write_number(worksheet, 2, 3, 2,   NULL);
    worksheet_write_number(worksheet, 3, 3, 3,   NULL);
    worksheet_write_number(worksheet, 4, 3, 5,   NULL);

    worksheet_set_column(worksheet, COLS("A:D"), 11, NULL);

    chart_add_series(chart,
         "=Sheet1!$A$2:$A$5",
         "=Sheet1!$B$2:$B$5"
    );

    chart_add_series(chart,
         "=Sheet1!$A$2:$A$5",
         "=Sheet1!$C$2:$C$5"
    );

    chart_add_series(chart,
         "=Sheet1!$A$2:$A$5",
         "=Sheet1!$D$2:$D$5"
    );

    worksheet_insert_chart(worksheet, CELL("E9"), chart);

    return workbook_close(workbook);
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
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;
}
Exemplo n.º 6
0
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);
}
Exemplo n.º 7
0
int main() {

    lxw_workbook  *workbook  = workbook_new("test_row_col_format15.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    lxw_format    *italic      = workbook_add_format(workbook);
    format_set_italic(italic);

    worksheet_set_column(worksheet, 16383, 16383, 8.43, italic);

    return workbook_close(workbook);
}
Exemplo n.º 8
0
int main() {

    lxw_workbook  *workbook  = workbook_new("test_image18.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    lxw_image_options options = {.x_offset = 5, .y_offset = 5};

    worksheet_set_row(worksheet, 1, 96, NULL);
    worksheet_set_column(worksheet, 2, 2, 18, NULL);

    worksheet_insert_image_opt(worksheet, CELL("C2"), "images/issue32.png", &options);

    return workbook_close(workbook);
}
Exemplo n.º 9
0
int main() {

    lxw_workbook_options options = {1, NULL};

    lxw_workbook  *workbook  = workbook_new_opt("test_optimize22.xlsx", &options);
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_format    *bold      = workbook_add_format(workbook);

    format_set_bold(bold);

    worksheet_set_column(worksheet, 0, 0, 36, bold);

    return workbook_close(workbook);
}
Exemplo n.º 10
0
int main() {

    lxw_workbook  *workbook  = new_workbook("test_properties04.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_datetime   datetime  = {2016, 12, 12,  23, 0, 0};

    workbook_set_custom_property_string  (workbook, "Checked by",     "Adam");
    workbook_set_custom_property_datetime(workbook, "Date completed",  &datetime);
    workbook_set_custom_property_integer (workbook, "Document number", 12345);
    workbook_set_custom_property_number  (workbook, "Reference",       1.2345);
    workbook_set_custom_property_boolean (workbook, "Source",          1);
    workbook_set_custom_property_boolean (workbook, "Status",          0);
    workbook_set_custom_property_string  (workbook, "Department",      "Finance");
    workbook_set_custom_property_number  (workbook, "Group",           1.2345678901234);

    worksheet_set_column(worksheet, 0, 0, 70, NULL);
    worksheet_write_string(worksheet, CELL("A1"), "Select 'Office Button -> Prepare -> Properties' to see the file properties." , NULL);

    return workbook_close(workbook);
}
Exemplo n.º 11
0
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;
}