简体   繁体   中英

Qt connect to XLSX file with ODBC driver

I have that code, that I took from Qt wiki:

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Excel Driver (*.xls)};DBQ=" + QString("sht.xlsx"));
if(db.open())
{
    QSqlQuery query("select * from [" + QString("Sheet1") + "$]");
    // Select range, place A1:B5 after $
    while (query.next())
    {
        QString column1= query.value(0).toString();
        qDebug() << column1;
    }
}
else {
    qDebug() << db.lastError().text();
}

But it doesn't connect :( I can't understand why:

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

It was nedeed tp specify full path and set full name as in ODBC:

DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};

As always stack was useless. Guys here can answer only on noob questions, every real problem will stay here without answer.

You must compile and put database driver dll in a subfolder named 'sqldrivers' near your .exe file. In your case this should be qsqlodbc*.dll. Asterisk is a Qt major version number.

you need to install the obdc excel driver for xlsx and add it in your connect string instead of the xls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM