Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear experts

a.) We know: Write something into clipboard where fields are separated by <tab> and lines separated by <cr> we can paste it easily to Excel.

b.) Now, in Excel we can edit a cell and with alt& Enter we can force a 'line break' in a cell

The question:
Does anybody knows if it is possible (or definitively not), to put something into clipboard in the way that one can force Excel to do something like b.)?
I mean that Excel makes a 'line break' inside a cell, controlled by clipboard data?
Or is this only possible using 'ole automation'?

[Edit]
@OriginalGriff doubts in answer 1: To make it clear, this copy to clipboard is explicitly under control of the user.

Thank you very much in advance.

What I have tried:

Sorry, nothing. As the subject shows, lazy Q
Posted
Updated 25-Jun-22 2:25am
v3

Yeah, you just drop a "\n" (assuming C#) into the string you want to put on the clipboard.
 
Share this answer
 
Comments
0x01AA 24-Jun-22 17:25pm    
Thank you, let me try it, even I don't think it works. I will give feedback.
0x01AA 24-Jun-22 18:13pm    
Just tested it and unfortunately it does not give the desired result while paste into excel.
Anyway thank you for your response.
Dave Kreskowiak 24-Jun-22 18:31pm    
Then try "\r\n".
0x01AA 24-Jun-22 19:12pm    
'\r\n' ends definitively in a new excel row.
But your approach with '\n' seems to be the right way, I only don't see at the moment where I'm wrong.

a.) I copied a cell from excel with 'line breaks' into clipboard
b.) Pasted that into a notepad and saved it to inspect with 'hex editor' (nothing special when inspecting with hex editor)
c.) Copied from that notepad into clipboard
d.) Paste into a virgin excel file --> Excel put it into one cell with 'line breaks'

After all that, from experience I know the problem seems to be on my side.
I just not yet recognize where I'm wrong.

Thank you very much for your help and have my 5.
0x01AA 25-Jun-22 10:34am    
Finally solved. It was so easy, simply surrounding the 'cell values' with double quotation marks solved it.
Finally I found the solution.
Simply surrounding the 'cell data' with a double quotation mark is enough and Excel will handle them as one cell.

string clipboardData= "\"Col A\"\t\"Col B\n2cnd\"\t\"Col C\"\r\n";
If you copy that to clipboard and paste it in Excel than you get this:
+-----+-----+-----+
|Col A|Col B|Col C|
|     |2cnd |     |
+-----+-----+-----+
 
Share this answer
 
More importantly, why are you going via the clipboard at all?
In Interop - where you talk directly to Excel from your code - it's simple: the LineFeed character'\n' is an "in-cell" line break

Going via the clipboard may seem simple but if your code did that to me, it would be uninstalled with extreme prejudice, and I'd be looking for my money back ... you don't put anything on the clipboard unless the user specifically asks you to.
 
Share this answer
 
Comments
0x01AA 24-Jun-22 16:14pm    
a.) Why by clipboard: Because I like to avoid ole auto
b.) Going via clipboard....it would be uninstalled with extreme prejudice:
Sorry what is wrong with copy to clipboard? Of course I don't put it to clipboard in a hiden way. It is an action which is controlled by the user via a context menu.

[Edit]
Btw. I'm pretty sure you press meanwhile in wordle the button 'Share', which copies 'something' to your clipboard. Explain me the difference to my approach ;)
OriginalGriff 25-Jun-22 1:01am    
If I have data on the clipboard, it's because I want it there - I'm going to use it soon. If you app then bounces up and loads your data in the clipboard, mine ifs gone - and I'm going to be very annoyed when I paste my data and get your text instead of the image I just worked on for the last hour ...

And the "Share" button in Wordle copies to the clipboard when I press the button - knowing what it does! Your's is just using the clipboard to transfer info to Excel without user intervention because you don't want to use the "proper" mechanism - that's very different.
0x01AA 27-Jun-22 11:12am    
Thank you. Now the size is not that big, it is a filtered DataGrid (in worst case I could use a stream).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900