PDA

View Full Version : copy table to Excel



Arend
19th February 2013, 14:37
Hello,

I am a absolute beginner and I can't figure out how to copy a simple table to Excel, see the code below.
Can someone help me out?

Regards,
Arend



MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

QStandardItemModel *model = new QStandardItemModel(10,2,this);

model->setHorizontalHeaderItem(0, new QStandardItem("Column_1"));
for(size_t i=0;i<10;++i)
{
QModelIndex index = model -> index(i,0,QModelIndex());
model->setData(index,QString::number(i+1,'f',0));
}
model->setHorizontalHeaderItem(1, new QStandardItem("Column_2"));
for(size_t i=0;i<10;++i)
{
QModelIndex index = model -> index(i,1,QModelIndex());
model->setData(index,QString::number((i+1)*(i+1),'f',0));
}
ui->tableView->setModel(model);
}

Lykurg
19th February 2013, 14:49
Ctrl+A, Ctrl+C & Ctrl+V.

Or what exactly is not working. What have you tried. What result have you seen/loved to see.

Guess, try to define the QClipboard content yourself. E.g. a plain HTML table.

Arend
20th February 2013, 11:03
If I try Ctrl+A, Ctrl+C & Ctrl+V only the first value is copied.
I want to select columns and with Ctrl+C and Ctrl+V have a copy in Excel.
Below what I have tried so far.

Regards,
Arend



MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

table = new QTableView();
model = new QStandardItemModel(10,2,this);

connect (table, SIGNAL(clicked(QModelIndex)), this, SLOT(copy()));

model->setHorizontalHeaderItem(0, new QStandardItem("Column_1"));
for(size_t i=0;i<10;++i)
{
QModelIndex index = model -> index(i,0,QModelIndex());
model->setData(index,QString::number(i+1,'f',0));
}
model->setHorizontalHeaderItem(1, new QStandardItem("Column_2"));
for(size_t i=0;i<10;++i)
{
QModelIndex index = model -> index(i,1,QModelIndex());
model->setData(index,QString::number((i+1)*(i+1),'f',0));
}
table->setModel(model);
ui->tableView->setModel(model);
}

void MainWindow::copy()
{
QStringList list;
foreach (const QModelIndex& index, table->selectedIndexes() )
{
list << index.data() ;
}
clipboard->setText(list.join(", "));
}

Lykurg
20th February 2013, 11:07
That's the right way to go. I am not sure what Excel exactly supports but a normal csv file should work. So extend the copy slot and create a csv content for the clipboard and when you insert it in Excel you should be confronted with an csv import dialog.

Arend
20th February 2013, 11:20
Thanks for the reply.
But when I compile this I get:
... error: C2248: 'QTableView::selectedIndexes' : cannot access protected member declared in class 'QTableView'

mainwindow.h:


#include <QMainWindow>
#include <QtGui>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
QStandardItemModel *model;
QTableView *table;
void copy();
};

Lykurg
20th February 2013, 11:27
Use QAbstractItemView::selectionModel().

Arend
20th February 2013, 12:06
Thanks again for the reaction, but please can you give more explanation.
Regards,
Arend

Lykurg
20th February 2013, 12:11
table->selectionModel()->selectedIndexes()And then determine how many cols and rows the selection covers and construct the csv data. That's an exercise for you.

decipher
27th February 2013, 06:32
the below code i did for qtablewidget to csv file...


QString csv_file = QFileDialog::getSaveFileName(this, "Export CSV", "Print", "*.csv");
if (csv_file.isEmpty()) return;
if (QFileInfo(csv_file).suffix().isEmpty())
csv_file.append(".csv");
QFile f12(csv_file);
if (f12.open(QFile::WriteOnly | QFile::Truncate))
{
QTextStream data( &f12 );
QStringList strList;
strList.clear();

for( int c = 0; c < ui->tablewidget->columnCount(); c++ )

{


strList <<
"\" " +
ui->tablewidget->horizontalHeaderItem(c)->data(Qt::DisplayRole).toString() +
"\" ";


}
data << strList.join( "," )+"\n";
for( int r = 0; r < 25000; r++ )//row
{
strList.clear();
for( int c = 0; c < 10; c++ )//column
{
strList << "\""+ui->tablewidget->item( r, c )->text()+"\"";

}
data << strList.join( "," )+"\n";
}
f12.close();
}