Best way to populate a QTableView or QTableWidget with Excel data (urgent)



  • Hi, I'm working on an application where I need to import Excel data and put it into either a QTableView or QTableWidget. I'm wondering what would be the best method to accomplish this. Please i want just a sample code for this.

    1. Help me to import Excel files in Qt
      2.Populates a QTableView or QTableWidget with Excel data

    [[Merged two posts, Tobias]]



  • Hi, I'm working on an application where I need to import Excel data and put it into either a QTableView or QTableWidget. I'm wondering what would be the best method to accomplish this:
    Please i want just a sample code for this:

    1. Help me to import Excel files in Qt
      2.Populates a QTableView or QTableWidget with Excel data


  • You need

    a C++ library to perform the parsing of the Excel files (libxl?)

    to write a custom model (like, a QAbstractTableModel subclass) that uses that library in order to provide the data to Qt views (in your case, a QTableView).

    If you have an existing excel installation you could also use the ActiveX extension for Qt (search in the forums, I saw topics about this).



  • On flagging your question as urgent, please read "this":http://www.catb.org/~esr/faqs/smart-questions.html#urgent.



  • It's quite simple, you just have to do it.

    Okay, first, we need to export it to a CSV. (Comma separated values)

    Now, use QFile::open(QIODevice::ReadOnly) to read the csv file, read the lines.

    For each line use the QString::split(",") function to get list of values.

    Now use a QStandardItemModel to add the first line as header (QStandardItemModel::setHorizontalHeaderLabels (QStringList)), values for each row (QStandardItemModel::appendRow(QList<QStandardItem*>)).

    Set the model on a QTableView.

    Done. I don't want to write the code because it is trivial. But the pseudo-code should guide you, I hope.



  • Unfortunately, parsing real CSV is not trivial. Just split at "," does lead to false parts if one of the fields contains a comma itself. Usually the CSV is wrapped in qutoation marks (") in this case, which need be removed for retrieving back the actual value. I leave the implications of a field value containing a comma and a quotation mark to you. So no, this is actually not a good recommendation unless one uses a mature CSV parsing library.



  • @Volker: Hmm.. you're right. It needs more handling than I have suggested. Maybe, best to stick to an external library for parsing.



  • Thanks for answer.
    i already download libXl librairy but i have always problems.
    Please give me an a sample code and steps for reading/writing Excelfiles


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.