I am working on a project where I have to make a table from an excel file. The following code that is related to making a table from a CSV file, works fine. How can I convert my excel file to CSV in this code, in order to have a table from my excel file? Also, is there another way to make a table from an excel file, without converting it to a csv file?
I have used QFile to import my excel file.
I would appreciate if someone could help me on this matter. Here is the code:
Mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QStandardItemModel *csvModel;
};
#endif // MAINWINDOW_H
Mainwindow.cpp:
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
csvModel = new QStandardItemModel(this);
csvModel->setColumnCount(3);
csvModel->setHorizontalHeaderLabels(QStringList() << "Header1" << "Header2" << "Header3");
ui->tableView->setModel(csvModel);
QFile file("C:/Users/Haybert/Desktop/test.xlsx ");
if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
qDebug() << "File not exists";
} else {
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
QList<QStandardItem *> standardItemsList;
for (QString item : line.split(";")) {
standardItemsList.append(new QStandardItem(item));
}
csvModel->insertRow(csvModel->rowCount(), standardItemsList);
}
file.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
delete csvModel;
}