Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Paste data to Excel does not work, as I wish, with both CSV and text data on clipboard
QtWS25 Last Chance

Paste data to Excel does not work, as I wish, with both CSV and text data on clipboard

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tabula
    wrote on last edited by
    #1

    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.

    @{

    QString selectedCSV = "Fruit,Cars\n"
    "12,13\n"
    "14,15\n";
    QString selectedText = "Fruit Cars\n"
    " 12 13\n"
    " 14 15\n";

    QByteArray copyToClipboardText;
    copyToClipboardText.append(selectedText);

    QByteArray copyToClipboardCSV;
    copyToClipboardCSV.append(selectedCSV);

    QMimeData *mimeData = new QMimeData();

    // Use all mimetypes that Excel could possibly think of
    mimeData->setData ("CSV", copyToClipboardCSV);
    mimeData->setData ("Csv", copyToClipboardCSV);
    mimeData->setData ("csv", copyToClipboardCSV);
    mimeData->setData ("text/CSV",copyToClipboardCSV);
    mimeData->setData ("text/Csv",copyToClipboardCSV);
    mimeData->setData ("text/csv",copyToClipboardCSV);

    mimeData->setData ("text/plain", copyToClipboardText);

    QApplication::clipboard ()->setMimeData(mimeData);

    }@

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      just specifying the mime-type in lower case is enough. So you just need text/cvs and text/plain.

      You can just set a preference for the data to choose (by order, like you already did). Which format is then finally taken is part of the target application only.

      But what yo could try is:
      Copy some cells out of excel and check what type the desired format has (as long as it is saved as plain-text) and use this type.
      If it's saved as binary you probably wont have a chance to copy the format into your own application.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved