首页 > 代码库 > qt通过odbc操作Excel

qt通过odbc操作Excel

1、查找资料却没找到能够通过odbc访问07版以上的Excel

 

2、代码

#include "widget.h"#include <QApplication>#include <QtSql/QSqlDatabase>#include <QtSql/QSqlQuery>#include <QDebug>int main(int argc, char *argv[]){    QApplication a(argc, argv);    Widget w;    w.show();    int index=0;    //odbc    QSqlDatabase dbExcel = QSqlDatabase::addDatabase("QODBC");    //dbExcel.setDatabaseName("Driver=jdbc:odbc:DbExcel;ReadOnly=False;");    //dbExcel.setDatabaseName("Driver={Microsoft Excel Driver (*.xls,*.xlsx,*.xlsm,*.xlsb)};Readonly=0;Dbq=D:\\test.xlsx;DefaultDir=D:\\");    dbExcel.setDatabaseName("Driver={Microsoft Excel Driver (*.xls)};Readonly=0;Dbq=D:\\test2.xls;DefaultDir=D:\\");    if (dbExcel.open())        {            QSqlQuery query;            bool flag = query.exec(QStringLiteral("select * from [Sheet1$]"));            while (query.next()) {                QString title = query.value(0).toString();                QString author = query.value(1).toString();                qDebug()<< title;                qDebug()<< author;            }        }else{        qDebug()<< "EXCEL ERROR";    }    return a.exec();}

 

qt通过odbc操作Excel