快速开始(C)

本节用 C 演示了一个简单的用例,展示了如何使用 oosxl 库函数,例如创建 xlsx 文档,设置单元格内容,以及设置单元格格式等方法。

第一步,包含 oosxl 库的头文件:

#include "oosxl.h"

第二步,在main函数中,使用无参的 create_document() 函数创建一个空白文档或者使用 load_document() 从某个 xlsx 文件中加载文档。

DocumentHandle doc = doc_create(); // Create a blank document
// or
// DocumentHandle doc = doc_load("path/to/xlsx/file");

如果加载不存在的文件,load_document() 会返回一个空指针,你可以使用下面的方法做出处理:

if (!doc)
{
    std::cout << run_msg() << std::endl;
    //...
}

备注

oosxl 库不会向外传播异常,要获得程序错误,应使用 run_msg() 函数输出的字符串获取相应的信息(空字符串表示没有错误)。run_msg() 输出的是上一次的错误或提示信息(如果有)。

create_document() 或 load_document() 函数返回一个 DocumentHandle (示例中的 doc), 此句柄所指向的实例中隐含了一个 Workbook 句柄,指向 xlsx 文档中的 workbook 实例:

WorkbookHandle wb = doc_get_workbook(doc);

第三步,创建或获取 worksheet。

使用 wb_add_worksheet() 创建一个新的 worksheet:

// Create an auto-named worksheet
WorksheetHandle ws1 = wb_add_worksheet(wb);
// Create a worksheet named "My Sheet"
WorksheetHandle ws2 = wb_add_worksheet(wb, "My Sheet");

使用 wb_get_sheet() 获取 worksheet, 参数为基于 0 的位置索引:

// Get the first econd worksheet
WorksheetHandle ws = wb_get_sheet(0);

现在,使用返回的 Worksheet 句柄 ws,我们就可以与其所指向的 worksheet 实例中的单元格进行交互了。

下面是一个完整的 C 程序示例,展示了 oosxl 库的使用方法:

// Step 1: include the header
#include "oosxl.h"

#include <iostream>
#include <vector>

int test_example()
{
    // Step 2: create a document
    DocumentHandle doc = doc_create();
    if (!doc)
    {
        std::cout << "Error creating document: " << run_msg() << std::endl;
        return -1;
    }

    // Step 3: Get the workbook and worksheet
    WorkbookHandle wb = doc_get_workbook(doc);
    WorksheetHandle ws = wb_add_worksheet(wb); // Get the worksheet
    if (!ws)
    {
        std::cout << "Error adding worksheet: " << run_msg() << std::endl;
        doc_release(doc);
        return -1;
    }

    // Set titles
    std::vector<std::string> titles = { "Item", "Units", "Unit Cost", "Total" };
    for (int col = 0; col < titles.size(); ++col)
        ws_set_text(ws, 2, col + 2, titles[col].c_str());

    // Set the first column
    std::vector<std::string> column1 = { "Baby doll", "Kite", "Toy guitar" };
    for (int row = 0; row < 3; ++row)
        ws_set_text(ws, row + 3, 2, column1[row].c_str());

    // Set the second column
    std::vector<int> column2 = { 95, 60, 75 };
    for (int row = 0; row < 3; ++row)
        ws_set_num(ws, row + 3, 3, column2[row]);

    // Set the third column
    std::vector<double> column3 = { 1.99, 2.99, 6.99 };
    for (int row = 0; row < 3; ++row)
        ws_set_num(ws, row + 3, 4, column3[row]);

    // Set the fourth column
    ws_set_formula(ws, 3, 5, "=C3*D3");
    ws_set_formula(ws, 4, 5, "=C4*D4");
    ws_set_formula(ws, 5, 5, "=C5*D5");

    // Merge cell(6,2) - cell(6,4)
    ws_merge(ws, 6, 2, 6, 4);
    ws_set_text(ws, 6, 2, "Total");

    // Set total
    ws_set_formula(ws, 6, 5, "=SUM(E3:E5)");

    StyleHandle fmt = wb_make_normal_style(wb);

    // Add borders
    style_set_border(fmt, BORDERINDEX_EDGE, BORDER_THICK);
    style_set_border(fmt, BORDERINDEX_INSIDE, BORDER_THIN);
    style_set_range_style(fmt, 2, 2, 6, 5, fmt);

    // Set title style
    style_unapply_all(fmt);
    style_set_align_h(fmt, HALIGN_CENTER);
    style_set_align_v(fmt, VALIGN_CENTER);
    style_set_pattern_fg_color(fmt, 0xDDDDDD);
    style_set_fill_pattern(fmt, PATTERNTYPE_SOLID);
    ws_set_range_style(ws, 2, 2, 2, 5, fmt);

    // Save document
    if (!doc_save(doc, "example.xlsx"))
    {
        std::cout << "Error saving file: " << run_msg() << std::endl;
        doc_release(doc);
        return -1;
    }

    doc_release(doc);
    return 0;
}

使用 microsoft Excel 应用程序打开 example.xlsx 文件,以验证输出结果:

../_images/quick-start-example.png