Hello,
First, I appologize for the length but I want to get all the facts
down so as not to waste any time :)
I'm trying to allow the user of my C# app to copy a DataGrid full of
double values to the clipboard (via ctrl-C, edit->copy, etc).
They can then paste these values wherever (but in particular, excel).
I've searched the web fairly exhaustively and cannot find an answer to
this. Every time someone asks, somone answers with the link I'm about
to post and that's the end of the thread. So either I'm doing
something wrong and the code in the link below works for everyone else
that has asked, or they all gave up in frustration. Supposedly one
can put a DataObject on the clipboard with a
DataFormat.CommaSeparatedValue type. There is example code to do this
at http://www.syncfusion.com/FAQ/WinFor...c98c.asp#q899q but it
does not seem to work for me. Can anyone confirm this? I'm running
win XP, excel 2002 sp-2 and using VS .NET. All I did was create a
simple window with 2 buttons (copy, paste) that call the two c#
functions from that web page. The *only* modificiation I made was to
write the results to a messageBox rather than console in the paste
function.
The results of my efforts were that:
1) If I press the copy button, then the paste button, I get a null
reference exception when creating the streamReader. This means that a
DataObject *is* getting on the clipboard and it *is* of type
DataFormat.CommaSeparatedValue but that when I try to GetData () it
gives back null. This seems to be confirmed by going to excel and
trying to paste. It allows paste (there is data on the clipboard) but
when I try to paste it gives me a MessageBox with "Excel cannot paste
this data". When doing a "paste special" it allows to me to select
which clipboard data to paste and it shows a "CSV" type dataObject,
but again gives the "cannot paste" message. This leads me to beleive
that there is a problem in the function that copies data to the
clipboard.
2) If I copy a selection of data IN excel, then press the paste button
on my test app, it gets and shows the data correctly, leading me to
beleive that the paste function is working semi-correctly (it does not
check for/catch the null reference when data is bad as in (1) above,
but no biggie, can fix that). Again, this leads me to think the
problem is in the copy function.
3) I posted the data to the clipboard as a comma-separated string,
with newlines between the rows, I just called Clipboard.SetDataObject
("1,2,3\n4,5,6");. Excel can then paste this data and reads the
newline correctly for rows, but does not separate the values on commas
into columns. If I change the commas to tabs, then it works
correctly. I discovered this is due to the setting in the Data->Text
to Columns options for excel. If you do a text-to-columns operation
with TAB as the delimiter, then it will correctly handle tabbed data
on the clipboard, but not comma data and vice-versa. So, I can get my
data into excel if I have to, though the user might have to run
text-to-columns on it if their text-to-columns delimiter was different
than the one I chose when they paste the data. This is of course
non-optimal since I should just be able to tell excel that the
clipboard data is in CSV format via DataFormat.CommaSeparatedValue and
have it work no matter what the user's delimiter for text-to-column
ops is.
Has anyone made this CSV on the clipboard thing work? am I way off
base? Do I have to live with option 3 above?
Thanks in advance,
Chris