当前位置:首页 » 《休闲阅读》 » 正文

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

1 人参与  2024年09月27日 12:00  分类 : 《休闲阅读》  评论

点击全文阅读


一、简介

官网: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 <xlnt/xlnt.hpp>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 << "正在读取工作表: " << ws.title() << std::endl;

3. 读取指定单元格

// 读取 A1 单元格的值auto cell_value = ws.cell("A1").value();auto cell_value = ws.cell(xlnt::cell_reference(1, 1)).value();auto cell_value = ws.cell(1, 1).value();

4. 获取行数或列数

// 获取所有行auto rows = ws.rows();// 计算行数std::size_t row_count = std::distance(rows.begin(), rows.end());// 获取所有列auto columns = ws.columns();// 计算行数std::size_t row_count = std::distance(columns.begin(), columns.end());

5. 读取指定范围

#include <xlnt/xlnt.hpp>#include <iostream>#include <vector>int main() {    xlnt::workbook wb;    wb.load("example.xlsx"); // 加载 Excel 文件    auto ws = wb.active_sheet();    std::vector<std::string> row_data;    // 读取第1行的数据    for (const auto& cell : ws.range("1:1")) {        row_data.push_back(cell.to_string());    }    // 打印第1行的数据    for (const auto& data : row_data) {        std::cout << data << " ";    }// 打印所有行for (auto row : ws.rows(true)) { // 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 << cell.value<std::string>() << std::endl; // write the text content of the cell        std::cout << cell.value<double>() << std::endl; // write the number content of the cell        std::cout << cell.to_string() << std::endl; // convert to a string according to number formatting    }}    return 0;}

6. 设置属性

// 设置表单名称ws.title("test");// 设置单元格文本的属性ws.cell("A1").font(xlnt::font().color(xlnt::color::red()));ws.cell("A1").font(xlnt::font().name("SimSun"));ws.cell("A1").font(xlnt::font().bold(true));ws.cell("A1").font(xlnt::font().size(13));// 同时设置单元格的多个属性ws.cell("A1").font(xlnt::font().color(xlnt::color::green()).name("SimSun").bold(true).size(20));// 设置单元格的背景颜色为绿色ws.cell("A1").fill(xlnt::fill::solid(xlnt::color::green()));ws.cell("A2").fill(xlnt::fill::solid(xlnt::rgb_color(0, 255, 0)));// 设置行高ws.row_properties(1).custom_height = true;// 必须设置为true才能设置行高ws.row_properties(1).height = 30;// 设置第1行高度为30 // 设置列宽ws.column_properties(1).width = 20; // 设置第1列宽度为20// 设置对齐方式cell.alignment(xlnt::alignment().horizontal(xlnt::horizontal_alignment::center));cell.alignment(xlnt::alignment().vertical(xlnt::vertical_alignment::top));// 合并C3:C4单元格ws.merge_cells("C3:C4");

7. 添加超链接

// 设置单元格文本ws.cell("A1").value(u8"点击打开百度");// 创建一个超链接ws.cell("A1").hyperlink("https://www.baidu.com/");

三、进阶用法

1. xlsx表格与xlnt库对象关系

xlsx对象xlnt对象说明
表格文件xlnt::workbook
表单xlnt::worksheet表单的标题在最下面
行rowrow_t从1开始
列columncolumn_t从A开始
单元cellxlnt::cell包含内容和字体等属性

四、注意事项

尽量对xlnt生成的excel文件进行修改,而不是其他软件生成的excel文件,否则程序可能会崩溃!
如果必须使用其他软件生成excel中的数据,可将数据粘贴到xlnt生成的excel文件中,再进行操作。

https://www.cpp-prog.com/编程/xlnt/
VS2022编译xlnt库


点击全文阅读


本文链接:http://zhangshiyu.com/post/165285.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 完结文我在恐怖游戏开裁缝铺列表_完结文我在恐怖游戏开裁缝铺(林慕秋)
  • 全书免费谢清禾姜博诚_谢清禾姜博诚全书免费
  • 离婚后我收获真爱,前妻却快死了(秦落音陆轩),离婚后我收获真爱,前妻却快死了
  • 老公把上亿豪宅送养妹后,我把人和房都拆了(顾思思顾言洲)全书免费_(顾思思顾言洲)老公把上亿豪宅送养妹后,我把人和房都拆了后续(顾思思顾言洲)
  • 沈星悦傅时安_沈星悦傅时安
  • 离婚后,居然还能以旧换新?(陈汉李淼淼李思)全书浏览_离婚后,居然还能以旧换新?全书浏览
  • 完结文给女团主播狂刷百万反被骂穷逼,我反手送她队友出道列表_完结文给女团主播狂刷百万反被骂穷逼,我反手送她队友出道(秦薇)
  • 全书浏览老公将我第十个孩子送给情人后,我果断改嫁他绝嗣干爹(苏云遮盛炽)_老公将我第十个孩子送给情人后,我果断改嫁他绝嗣干爹(苏云遮盛炽)全书结局
  • 全文无边怨恨是她活下来的最大动力(江寒静顾榕赫)列表_全文无边怨恨是她活下来的最大动力
  • 全文爸爸死后,消失二十年的妈妈带着儿子回来跟我争家产(顾霆锋顾青卿)列表_全文爸爸死后,消失二十年的妈妈带着儿子回来跟我争家产
  • 霍晚清赵旭然_霍晚清赵旭然
  • 弟弟看的破茧时光沉淀后的深情相拥林悦苏然全书在线

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1