Qt读取excel
来源:互联网 发布:python黑客编程pdf 编辑:程序博客网 时间:2024/05/21 16:12
Qt读取excel
使用 odbc 和 QtXlsxWriter 读 xls 和 xlsx 格式的 excel 文件,使用 odbc 读 xlsx 会链接失败,使用 QtXlsxWriter 读 xls 也无法读取,所以只好针对两种格式分别处理,使用 odbc 和 QtXlsxWriter 的好处是不用在目标机器上安装 excel 。
- 安装 QtXlsxWriter:
在 https://github.com/dbzhang800/QtXlsxWriter 下载源码。
下载安装 perl
使用Qt命令行cd到QtXlsxWriter目录,然后执行:
qmake make make install
mingw改成
qmake mingw32-make mingw32-make install
执行完毕就安装完成了。
- 读取:
在工程文件中加入:
QT += sql xlsx
头文件包含:
#include <QString>#include <QDebug>#include <QStandardPaths>#include <QFileDialog>#include <QSqlDatabase>#include <QMessageBox>#include <QSqlError>#include <QSqlQuery>#include <QtXlsx/QtXlsx>
cpp:
bool BP_ExcelImport::read_excel(){ QString desktopDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); QString filePath = QFileDialog::getOpenFileName(NULL,"选择导入文件",desktopDir,"*.xls;*.xlsx"); if(filePath.isNull()) { qDebug()<<"未选择导入文件."; return false; } if(0 == filePath.right(4).compare(".xls",Qt::CaseInsensitive)) return read_xls(filePath); else if(0 == filePath.right(5).compare(".xlsx",Qt::CaseInsensitive)) return read_xlsx(filePath); qDebug()<<QString("选择了非excel文件:") + filePath; return true;}//读 xlsxbool BP_ExcelImport::read_xlsx(const QString & path){ QXlsx::Document xlsx(path); QString s; for(int i = 0;i < 2;++i) for(int j = 1;j < 19;++j) s = s + xlsx.read(QString('A' + i) + QString::number(j)).toString() + "\r\n"; QMessageBox(QMessageBox::Information,"message",s).exec(); return true;}//读 xlsbool BP_ExcelImport::read_xls(const QString & path){ QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); QString dsn ="DRIVER={Microsoft Excel Driver (*.xls)};DSN='';DBQ=" + path; db.setDatabaseName(dsn); if(db.open()) { QSqlQuery query(db); query.exec("select * from [" + QString("Sheet1") + "$]"); QString s; while (query.next()) s = s + query.value(0).toString() + " " + query.value(1).toString() + "\r\n"; QMessageBox(QMessageBox::Information,"message",s).exec(); } else { QSqlError error = db.lastError(); qDebug()<<QString("xls db open failed:")+error.driverText()+("\r\nerror type:")+QString::number(error.type())+"\r\n"; return false; } return true;}
效果:
ODBC的方式读不出来excel的第一行,也没找到读取的方法,怀疑是第一行被认为是表头了。
阅读全文
0 0
- QT读取excel
- Qt读取Excel 一
- Qt读取excel表格
- QT读取excel
- Qt读取excel文件
- Qt读取excel
- Qt读取excel
- QT 读取 EXCEL XLS文件
- QT读取Excel 简单范例代码
- Qt通过ODBC读取excel文件
- Qt读取/写入Excel数据--QAxObject
- Qt中QAxObject读取Excel文件
- Qt用QAxObject读取excel编译出错问题解决
- QT学习之读取excel并保存为文本文件
- 学习Qt和MFC读取excel的经历
- QT快速读取数据量很大的Excel文件
- Qt用QAxObject读取excel编译出错问题解决
- 使用Qt从excel读取数据至数据库
- linux驱动子系统之输入子系统(1)
- eclipse 中git解决冲突
- 电商优惠活动
- WEB01_HTML
- 我常用的DB2 for z/OS DBA面试题及答案
- Qt读取excel
- Java default关键字
- mybatis在logback中配置输出执行sql
- [spring]启动时报错:NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
- 总结DetachedCriteria级联查询
- 关于Linux文件目录操作的12个常用命令 写在前面:
- 极其方便的使用Mybatis单表的增删改查
- pthread_cleanup_push() & pthread_cleanup_pop()
- java并发编程学习(五) java 内存模型的基础