Results 1 to 9 of 9

Thread: copy table to Excel

  1. #1
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default copy table to Excel

    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

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    2. {
    3. ui->setupUi(this);
    4.  
    5. QStandardItemModel *model = new QStandardItemModel(10,2,this);
    6.  
    7. model->setHorizontalHeaderItem(0, new QStandardItem("Column_1"));
    8. for(size_t i=0;i<10;++i)
    9. {
    10. QModelIndex index = model -> index(i,0,QModelIndex());
    11. model->setData(index,QString::number(i+1,'f',0));
    12. }
    13. model->setHorizontalHeaderItem(1, new QStandardItem("Column_2"));
    14. for(size_t i=0;i<10;++i)
    15. {
    16. QModelIndex index = model -> index(i,1,QModelIndex());
    17. model->setData(index,QString::number((i+1)*(i+1),'f',0));
    18. }
    19. ui->tableView->setModel(model);
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: copy table to Excel

    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.

  3. #3
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: copy table to Excel

    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

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    2. {
    3. ui->setupUi(this);
    4.  
    5. table = new QTableView();
    6. model = new QStandardItemModel(10,2,this);
    7.  
    8. connect (table, SIGNAL(clicked(QModelIndex)), this, SLOT(copy()));
    9.  
    10. model->setHorizontalHeaderItem(0, new QStandardItem("Column_1"));
    11. for(size_t i=0;i<10;++i)
    12. {
    13. QModelIndex index = model -> index(i,0,QModelIndex());
    14. model->setData(index,QString::number(i+1,'f',0));
    15. }
    16. model->setHorizontalHeaderItem(1, new QStandardItem("Column_2"));
    17. for(size_t i=0;i<10;++i)
    18. {
    19. QModelIndex index = model -> index(i,1,QModelIndex());
    20. model->setData(index,QString::number((i+1)*(i+1),'f',0));
    21. }
    22. table->setModel(model);
    23. ui->tableView->setModel(model);
    24. }
    25.  
    26. void MainWindow::copy()
    27. {
    28. foreach (const QModelIndex& index, table->selectedIndexes() )
    29. {
    30. list << index.data() ;
    31. }
    32. clipboard->setText(list.join(", "));
    33. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: copy table to Excel

    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.

  5. #5
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: copy table to Excel

    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:
    Qt Code:
    1. #include <QMainWindow>
    2. #include <QtGui>
    3.  
    4. namespace Ui {
    5. class MainWindow;
    6. }
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15.  
    16. private:
    17. Ui::MainWindow *ui;
    18. QTableView *table;
    19. void copy();
    20. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: copy table to Excel


  7. #7
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: copy table to Excel

    Thanks again for the reaction, but please can you give more explanation.
    Regards,
    Arend

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: copy table to Excel

    Qt Code:
    1. table->selectionModel()->selectedIndexes()
    To copy to clipboard, switch view to plain text mode 
    And then determine how many cols and rows the selection covers and construct the csv data. That's an exercise for you.

  9. #9
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: copy table to Excel

    the below code i did for qtablewidget to csv file...
    Qt Code:
    1. QString csv_file = QFileDialog::getSaveFileName(this, "Export CSV", "Print", "*.csv");
    2. if (csv_file.isEmpty()) return;
    3. if (QFileInfo(csv_file).suffix().isEmpty())
    4. csv_file.append(".csv");
    5. QFile f12(csv_file);
    6. if (f12.open(QFile::WriteOnly | QFile::Truncate))
    7. {
    8. QTextStream data( &f12 );
    9. QStringList strList;
    10. strList.clear();
    11.  
    12. for( int c = 0; c < ui->tablewidget->columnCount(); c++ )
    13.  
    14. {
    15.  
    16.  
    17. strList <<
    18. "\" " +
    19. ui->tablewidget->horizontalHeaderItem(c)->data(Qt::DisplayRole).toString() +
    20. "\" ";
    21.  
    22.  
    23. }
    24. data << strList.join( "," )+"\n";
    25. for( int r = 0; r < 25000; r++ )//row
    26. {
    27. strList.clear();
    28. for( int c = 0; c < 10; c++ )//column
    29. {
    30. strList << "\""+ui->tablewidget->item( r, c )->text()+"\"";
    31.  
    32. }
    33. data << strList.join( "," )+"\n";
    34. }
    35. f12.close();
    36. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. copy tableview to Excel
    By Arend in forum Newbie
    Replies: 3
    Last Post: 9th December 2012, 21:58
  2. copy/move excel sheet with QT
    By gerardpuducul in forum Qt Programming
    Replies: 16
    Last Post: 4th December 2012, 09:25
  3. Replies: 0
    Last Post: 29th September 2011, 08:32
  4. How to copy from Excel and paste to QTableView
    By sms in forum Qt Programming
    Replies: 5
    Last Post: 7th February 2009, 03:58
  5. Filtering as Excel in Table
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2006, 09:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.