Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. extract data from excel file using QODBC

extract data from excel file using QODBC

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    I use this code to extract data from excel file

    void MainWindow::connectToExcel()
    {
        QString excelFilePath = "D:/a.xls";
    
        dp = QSqlDatabase::addDatabase("QODBC");
        // set the dsn string
        QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls)};DSN='';FIRSTROWHASNAMES=1;READONLY=TRUE;DBQ=%1").
                      arg(excelFilePath);
        dp.setDatabaseName(dsn);
        if(!dp.open())
        {
            qDebug() << "Error " << dp.lastError().text() << endl;
            return;
        }
        QSqlQuery q;
        q.prepare("SELECT * FROM [test$a1:B]");
        if (!q.exec()){
            qDebug() << "Error = " << q.lastError().text();
            return;
        }
        while (q.next()){
            qDebug() << q.value(0).toString() << " " << q.value(1).toString();
        }
    }
    

    it works fine with .xls but if i change it to use it on an excel file with this extension .xlsx it didn't work and give me this error

    "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"
    

    this there is a solution for this problem ?

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Perhaps you need to update Microsoft's ODBC driver for Excel, the old one only supports .xls files, you can check if you start ODBC Admin. More discussion here

      1 Reply Last reply
      3
      • K Offline
        K Offline
        kenchan
        wrote on last edited by
        #3

        your driver string should probably be (Microsoft Excel Driver (*.xls , *.xlsx, *.xlsm, *.xlsb) or what ever you see in your windows ODBC driver gui when you run odbcad32.exe for the architecture you want to use.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved