小工具知识网

当前位置: 首页 > 行业知识 > C++读写Excel(xlnt库的使用)

行业知识

C++读写Excel(xlnt库的使用)

2026-11-16 08:28:31 来源:互联网转载

一、简介

官���:https://github.com/tfussell/xlnt

()

Cross-platform user-friendly xlsx library for C++11+

xlnt is a modern C++ library for manipulating spreadsheets in memory and reading/writing them from/to XLSX files as described in ECMA 376 4th edition. The first public release of xlnt version 1.0 was on May 10th, 2017. Current work is focused on increasing compatibility, improving performance, and brainstorming future development goals. For a high-level summary of what you can do with this library, see the feature list. Contributions are welcome in the form of pull requests or discussions on the repository’s Issues page.

()

xlnt 是一个现代 C++ 库,用于在内存中操作电子表格,并按照 ECMA 376 第 4 版中的描述,从 XLSX 文件中读取/写入电子表格。xlnt 1.0 版本于 2017 年 5 月 10 日首次公开发布。目前的工作重点是增强兼容性、提高性能,以及集思广益制定未来的开发目标。

二、基本用法

1. 生成excel

#include int main(){    xlnt::workbook wb;    xlnt::worksheet ws = wb.active_sheet();    ws.cell("A1").value(5);// 写入数值    ws.cell("B2").value("string data");// 写入字符串    ws.cell("C3").formula("=RAND()");// 写入公式    ws.merge_cells("C3:C4");// 合并C3:C4单元格    ws.freeze_panes("B2");// 冻结B2    wb.save("example.xlsx");    return 0;}

2. 读取指定表单

xlnt::workbook wb;wb.load("example.xlsx"); // 加载 Excel 文件// 通过名称获取指定的工作表xlnt::worksheet ws = wb.sheet_by_title("Sheet1");// xlnt::worksheet ws = wb.sheet_by_title(u8"中文");// 也可以通过索引获取指定的工作表,索引从 0 开始// xlnt::worksheet ws = wb.sheet_by_index(0);// xlnt::worksheet ws = wb.sheet_by_id(0);std::cout     xlnt::workbook wb;    wb.load("example.xlsx"); // 加载 Excel 文件    auto ws = wb.active_sheet();    std::vector        row_data.push_back(cell.to_string());    }    // 打印第1行的数据    for (const auto& data : row_data) {        std::cout  // or false, see above    for (auto cell : row) { // no need for reference, cell/row are just wrappers around pointers        // the following are three different ways to print the content of the cell        std::cout 

上一篇:揭秘百度推广优化秘籍,高效引流策略助你提升广告转化率!

下一篇:揭秘下载63723背后的法律风险与犯罪问题解析