Results 1 to 2 of 2

Thread: Paste data to Excel does not work with both CSV and text data on clipboard

  1. #1
    Join Date
    Sep 2008
    Posts
    54
    Thanks
    3
    Thanked 10 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Paste data to Excel does not work with both CSV and text data on clipboard

    Hello,

    I am trying to get my data on the clipboard so it can be past it in a text editor and a spreadsheet program. In both situations I would like the paste to be a single click; in contrast with the paste special of Excel that allows you to select a different format from the clipboard.

    I have the code below that adds both CSV format and text format on the clipboard.

    If I paste it in Excel it looks like the text format is chosen and thus I loose my formatting. In notepad++ I get what I expect.

    Using ClipBoardViewer I can see my data on the clipboard in the way I expect it.

    Anyone has experience with this situation?

    How can I get both text and CSV format on the clipboard so that a text editor chooses the text and a spreadsheet program uses the CSV format?

    For me converting the data to HTML is not an option. I say this because I tested html format and it looks that that type get precedence over text while pasting into Excel.

    Qt Code:
    1. {
    2. QString selectedCSV = "Fruit,Cars\n" \
    3. "12,13\n" \
    4. "14,15\n";
    5. QString selectedText = "Fruit Cars\n" \
    6. " 12 13\n" \
    7. " 14 15\n";
    8.  
    9. QByteArray copyToClipboardText;
    10. copyToClipboardText.append(selectedText);
    11.  
    12. QByteArray copyToClipboardCSV;
    13. copyToClipboardCSV.append(selectedCSV);
    14.  
    15. QMimeData *mimeData = new QMimeData();
    16.  
    17. // Use all mimetypes that Excel could possibly think of
    18. mimeData->setData ("CSV", copyToClipboardCSV);
    19. mimeData->setData ("Csv", copyToClipboardCSV);
    20. mimeData->setData ("csv", copyToClipboardCSV);
    21. mimeData->setData ("text/CSV",copyToClipboardCSV);
    22. mimeData->setData ("text/Csv",copyToClipboardCSV);
    23. mimeData->setData ("text/csv",copyToClipboardCSV);
    24.  
    25. mimeData->setData ("text/plain", copyToClipboardText);
    26.  
    27. QApplication::clipboard ()->setMimeData(mimeData);
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Paste data to Excel does not work with both CSV and text data on clipboard

    You could try a second format that is not text but understood by Excel.

    Or you could try using \t as the separator and use the resulting text for both targets (the editor should be able to handle tabs, Excel should be able to recognize that as CSV with tab as separator).

    Cheers,
    _

Similar Threads

  1. Data exporting to Excel error
    By rex in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2011, 16:38
  2. Replies: 0
    Last Post: 15th July 2010, 23:41
  3. Excel data import
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2010, 11:31
  4. copy - paste clipboard contents
    By viswanathan in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 12:10
  5. Replies: 1
    Last Post: 18th December 2006, 10:40

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.