PDA

View Full Version : Paste data to Excel does not work with both CSV and text data on clipboard



mstegehu
26th November 2013, 06:42
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);
}

anda_skoa
26th November 2013, 20:25
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,
_