2

I am doing an app for a client and he wants to read data from .xlsx, .csv and .xls.

For CSV files, I have already created a data parser, and for XLSX files, I had used QXlsx.

With large files (>1M lines), the library for XLSX files starts to run slowly, so I did some research and found this way to read .xlsx and .xls files in the Qt documentation https://wiki.qt.io/Handling_Microsoft_Excel_file_format

The problem I have is that ODBC always assumes I have a header even if I set ‘HDR=NO’.

Example:

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
db.setDatabaseName(
        "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};"
        "DBQ=C:/Workspace/Playground/TestXls.xls;"
        "HDR=NO;"
        );

QSqlQuery query("select * from [" + QString("Hoja1") + "$A:A]",db);

while (query.next())
{
        QString column1= query.value(0).toString();

        if (column1.isEmpty()) {
            break;
        }

        qDebug() << column1;    // this first print is A2 instead of A1
}

query.clear();
db.close();
QSqlDatabase::removeDatabase("xlsx_connection");

I also tried putting "Extended Properties=\"Excel 12.0;HDR=No;\"" instead of

"HDR=NO;".

Or Selecting the whole sheet and with a while read all data

QSqlQuery query("SELECT * FROM [Hoja1$]", db);

int currentRow = 0;

while (query.next()) {

    qDebug() << "Row" << currentRow << ":";
    for (int i = 0; i < query.record().count(); ++i) {
        qDebug() << "  Col" << i << ":" << query.value(i).toString();    // prints A2 as first row and first col instead of A1
    }

    ++currentRow;
}
3

0

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.