6

How could I properly connect to Excel file using QODBC/QODBC3 Qt drivers?

At firs I built a driver like that (in cmd)

>cd %QTPATH%\src\plugins\sqldrivers\odbc

>qmake odbc.pro

>nmake

then qsqlodbc4.dll file has been created in %QTPATH%\plugins\sqldrivers

I used this code

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

and there I cathed that error (it translates like:

[Microsoft][ODBC driver manager] Data source not found and default driver not set. QODBC3: Unable to connect

)

error

UPD

Also it says that drivers are avalible

QSqlDatabase: available drivers: QIBASE QSQLITE QODBC3 QODBC
10
  • You will definitly need to use the DSN call dbExcel.setDatabaseName(QString("DRIVER={Mic.... Commented Oct 16, 2013 at 6:34
  • @SebastianLange well I tried like this dbExcel.setDatabaseName(QString("DRIVER={Microsoft Excel Driver (*.xlsx)}; READONLY=FALSE; FIL={MS Excel}; DBQ=C:\databases\test.xlsx")); but it didn't worked. also made it uncommented in code to not confuse people anymore :) Commented Oct 16, 2013 at 6:39
  • @DanilGholtsman, did you get this to work for you? Commented Feb 20, 2015 at 17:48
  • 1
    @Rachael well, I used QAxObject stuff, as far as I remember qt-project.org/wiki/Using_ActiveX_Object_in_QT qtforum.org/article/22557/activeqt-excel.html Commented Feb 20, 2015 at 17:55
  • 1
    Ah! You've made my weekend. Thank you. Another curious question. Are you using MS SQL with QT and if so, how have you been successful at connecting to it without ODBC? Commented Feb 20, 2015 at 18:01

1 Answer 1

0

Wrong: DBQ=C:\databases\test.xlsx

Correct: Use double \ in addr

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.