Results 1 to 17 of 17

Thread: My approach to export QTableView data to a Microsoft Excel file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    5
    Thanked 7 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default My approach to export QTableView data to a Microsoft Excel file

    My approach to export QTableView data to a Microsoft Excel file

    Hi all. I’m from Beijing, China. I started using Qt 3 months ago. Before that I worked with MFC for more than 10 years. For me, programming with Qt is a whole new exiting experience. I want to thank all Qt developers for making this ‘cute’ library available.

    When I use QTableView to present data of a QSqlQueryModel, I think it’s necessary that the data can be exported to a Microsoft Excel file. So other people can add notes, print and copy the data easily. Since I’ve done the same thing before in MFC, I think maybe I can port it to Qt.

    After getting familiar with QtSql, it only took me 2 hours to finish the class “ExportExcelObject”. It’s easy to use.

    Qt Code:
    1. // 1. declare an object
    2. // – fileName contains the path to the Excel file
    3. // – sheetName is the name of the Excel sheet
    4. // – tableView is the pointer to a QTableView
    5. ExportExcelObject obj(fileName, sheetName, tableView);
    6.  
    7. // 2. define fields (columns) to the Excel sheet file
    8. // – the first parameter is referred to the column number in QTableView
    9. // – the second parameter is the name of the column of the Excel sheet
    10. // – the third parameter is the type of this column, you can use char(x) (max(x) is 255)
    11. // int, datetime, etc
    12. obj.addField(1, tr("name"), "char(60)");
    13. obj.addField(2, tr("ID"), "int");
    14. obj.addField(3, tr("time"), " datetime ");
    15.  
    16. // 3. connect the signal “exportedRowCount” to a progress widget to show the exporting
    17. // progress if necessary, this makes the exporting progress more user-friendly
    18. connect(&obj, SIGNAL(exportedRowCount(int)), progressBar, SLOT(setValue(int)));
    19.  
    20. // 4. do the work
    21. int retVal = obj.export2Excel();
    22. if(retVal > 0)
    23. {//done
    24. }
    25. else
    26. {//something wrong
    27. [FONT=Calibri] }[/FONT]
    To copy to clipboard, switch view to plain text mode 

    How does it work?
    1. Treat an Excel file as a database
    Use the DSN string below to create an Excel file:
    Qt Code:
    1. QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls)};DSN=''; FIRSTROWHASNAMES=1; READONLY=FALSE;CREATE_DB=\"%1\";DBQ=%2").
    2. arg(excelFilePath).arg(excelFilePath);
    To copy to clipboard, switch view to plain text mode 
    You don’t need to know the detail. {Microsoft Excel Driver (*.xls)} will do all the work.

    2. Treat a sheet as a table
    Just create an Excel sheet using SQL clause “CREATE TABLE” as you are creating a database table.

    3. Insert data to the table
    You know what to do.

    4. Unicode support
    Yes. Both the column names and contents support Unicode.

    excelexport.png
    I created a sample project (with Qt4.6.0) to demonstrate the code. Download hereTestTableView.zip(It's been compressed by WinRAR).
    It’s been tested on WinXP/Vista/7. It does NOT require an installed Excel because the driver is supported by Windows since Windows 2000.

  2. The following 7 users say thank you to venomj for this useful post:

    alizadeh91 (10th March 2013), jcyangzh (14th August 2012), qks1 (7th July 2013), setoy (17th January 2013), spartakuskus (18th October 2012), warcraff123456 (24th February 2013)

Similar Threads

  1. Replies: 3
    Last Post: 1st February 2011, 11:57
  2. How to read Excel file with Qt
    By HelloDan in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2009, 20:27
  3. How to export data from QTableView to Ms Excell
    By joseph in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2009, 14:19
  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. Replies: 3
    Last Post: 12th June 2008, 11:59

Tags for this Thread

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.