Read Excel data with Qt Creator

1

I would like to know how I could read the data of an excel from a Qt application. I've tried it with the following code but it reads data without sense.

#include <QFile>
#include <QStreamText>

int main(){
    QFile archivo("datos.xlsx");
    archivo.open(QIODevice::ReadOnly);
    QTextStream datos(&archivo);
    QString dato_actual;

    while(!datos.atEnd()){
       dato_actual = datos.readLine();
    }

    archivo.close();
}

The Excel file I want to read consists of two columns, in column "A" there are data of type Date (dd // mm / yyyy) and in column "B" there are integers.

Thank you!

    
asked by Adrian 06.11.2017 в 12:17
source

2 answers

5

To read an Excel file you have to bear in mind that you can find two different formats:

  • new format (2007 onwards).
  • old format (until 2003).

The new format (XLSX extension) is a compressed file with several XML in it (try to open an XLSX with a compressor). The old format, on the other hand, is a binary format.

This detail may or may not be important ... you do not specify it in your question, so I leave it for you just in case.

To read an Excel from Qt you have several options at your fingertips:

By hand

If you work on the routine to decompress the XLSX file and dedicate some time to the format you will see that accessing the data you need can be as simple as reading an XML from Qt (with DOM, at a low level, ... more you like).

ODBC

You can use the excel book as if it were a database. In this way, reading information is as simple (or complicated) as launching the relevant SQL query.

In order to use ODBC, you must have installed the Access database engine. Given the possibilities offered by this option, I will not elaborate much more ... there are many examples on the Internet.

Use third-party libraries

Apart from the already mentioned libXL , another good candidate is (since you're with Qt), QtXlsx . This library, being designed with Qt as a base, should integrate fairly well into your project.

    
answered by 06.11.2017 / 15:40
source
2

First, your algorithm will read your xlsx file as if it were a written text file (* .txt).

To be able to read the xlsx file you will need the Excel reading and writing library: #include "libxl.h" You can download it on its website: link

    
answered by 06.11.2017 в 15:12