如何使用 QODBC/QODBC3 Qt 驱动程序正确连接到 Excel 文件?
起初我建立了一个这样的驱动程序(在cmd中)
>cd %QTPATH%\src\plugins\sqldrivers\odbc
>qmake odbc.pro
>nmake
然后qsqlodbc4.dll
文件已在 %QTPATH%\plugins\sqldrivers
我用了这段代码
#include <QApplication>
#include <QtGui>
#include <QtSql>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
//QComboBox myCombo;
//excel stuff
QSqlDatabase dbExcel = QSqlDatabase::addDatabase("QODBC","dbExcel");
// i thought mayde that would work
// dbExcel.setDatabaseName("C:\databases\test.xlsx");
//i tried connection string too
dbExcel.setDatabaseName(QString("DRIVER={Microsoft Excel Driver (*.xlsx)}; READONLY=FALSE; FIL={MS Excel}; DBQ=C:\databases\test.xlsx"));
if(!dbExcel.open())
{
QSqlError er = dbExcel.lastError();
QMessageBox::information(0, "Error", er.text());
}
//...................
return app.exec();
}
在那里我发现了那个错误(它翻译成:
[Microsoft][ODBC driver manager] Data source not found and default driver not set. QODBC3: Unable to connect
)
UPD
它还说驱动程序可用
QSqlDatabase: available drivers: QIBASE QSQLITE QODBC3 QODBC