简介:
QT 自身带的QAxObject读写Excel, (1) 慢; (2) 使用起来不太方便;
1. 下载源码
https://github.com/QtExcel/Qxlnt
2. 将下图两个文件夹拷贝到新的工程目录
3. 新的工程文件包含Qxlnt.pri
1 2 | ###新项目的工程文件添加 include($$PWD/Qxlnt/Qxlnt.pri) |
4. 解决移植问题, 找不到文件
打开 Qxlnt/Qxlnt.pri 文件
将 ../xlnt/ 全部替换成 $$PWD/../xlnt/
再试试编译, 通过
5. 实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | //1. Writing a excel(xlsx) file #include <iostream> #include <xlnt/xlnt.hpp> #include <QCoreApplication> int main(int argc, char **argv) { QCoreApplication a(argc, argv); // It is a Qt code. xlnt::workbook wb; // It is a xlnt code. Mix it together! 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"); ws.freeze_panes("B2"); wb.save("example.xlsx"); return 0; } //2. Reading from an existing xlsx spread sheet. // https://tfussell.gitbooks.io/xlnt/content/docs/introduction/Examples.html xlnt::workbook wb; wb.load("/home/timothymccallum/test.xlsx"); auto ws = wb.active_sheet(); std::clog << "Processing spread sheet" << std::endl; for (auto row : ws.rows(false)) { for (auto cell : row) { std::clog << cell.to_string() << std::endl; } } std::clog << "Processing complete" << std::endl; |
或者本人搭建的一个简单的使用实例,其实基本教程都在这了, 下面还有官网源码, 例子,自己搭建更有趣
https://download.csdn.net/download/halo_hsuh/12312324
Note:
1. 文件不存在抛出异常, 需要捕获或者提前判断;
2. 解析时间下列连接中有例子;
3. 目前我只测了xls和xlsx, 只支持xlsx.
参考:
库码来源: https://github.com/QtExcel/Qxlnt
实例: https://tfussell.gitbooks.io/xlnt/content/docs/advanced/Properties.html