M
M
Maxim Kuprashevich2015-02-17 22:28:01
Qt
Maxim Kuprashevich, 2015-02-17 22:28:01

Qt how to connect to .xlsx via ODBC?

Hello, there is a code (taken 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();
}

And for some reason it throws an error:
"[Microsoft][ODBC Driver Manager] No data source found and no QODBC3 default driver specified: Unable to connect"

I've already broken my brain, why. Does anyone have any idea?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2015-02-18
@UndeadDragon

You need the full path to the xlsx file. The driver name must exactly match the name in ODBC.
You also need a working directory. In general, I understand something like
or

DBQ=c:\temp\dexcel003.xlsx;DefaultDir=c:\temp;Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DriverId=1046;FIL=Excel 12.0;MaxBufferSize=2048;PageTimeout=5;ReadOnly=0;

L
Lolshto, 2015-02-17
@Lol4t0

Do you have a driver?
hdi.simbahiveodbc.datasourceadmin1.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question