Important: Please read the Qt Code of Conduct -

Paste data to Excel does not work, as I wish, 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.


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

    QByteArray copyToClipboardText;

    QByteArray copyToClipboardCSV;

    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);


  • Moderators

    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.

Log in to reply