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

 http://qt-project.org/wiki/Handling_Microsoft_Excel_file_format

里边的odbc代码未运行成功。

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();
}

 

posted on 2015-02-02 18:06  充实自己  阅读(1015)  评论(0编辑  收藏  举报