Question

Thanks for any help in advance :)

Context I am using SpreadsheetGear within my Windows Application and there are certain cases where a user will want to copy data from an open Excel application and paste the two dimensional grid into the SpreadsheetGear object in my application.

Motivation I'm attempting to acquire information from the data in the clipboard in preparation for the paste to happen. The numbers of rows and columns of the data coming in needs to be determined before the paste happens so that the SpreadsheetGear control and other controls on the page are "ready" for the data.

Problem 1 How do I acquire such data from the Clipboard? I'm using

System.Windows.Forms.Clipboard.GetData(...)

but I'm not sure whether I should indicate the DataFormat to be CommaSeparatedValue (CSV) or Text. Will one way or the other work best? Is there another DataFormat that I am overlooking that could help me out?

Problem 2 I used this statement in the Immediate Window of Visual Studio 2012:

System.Diagnostics.Debug.WriteLine(Clipboard.GetText())

Interestingly, this returned a portion of the data I selected and copied in Excel. Is there a limit to the amount of data that the clipboard can handle from Excel? Or is there a way for my Windows App to help allocate more space on the clipboard, knowing the user is about the select data from Excel and copy that data to the clipboard?

Please let me know if I can provide more clarification. I'm a little lost and not sure about the scope of this issue. Thanks!

Was it helpful?

Solution

Here is what ended up working for me:

try
{
    var data = Clipboard.GetDataObject();
    var stream = (System.IO.Stream)data.GetData(DataFormats.CommaSeparatedValue);
    var enc = new System.Text.UTF8Encoding();
    var reader = new System.IO.StreamReader(stream, enc);
    string data_csv = reader.ReadToEnd();
    string[] data_csv_array = data_csv.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
    ...
}
catch (Exception e)
{
    ErrorHandling.ShowError("There is no data on the Clipboard to paste.");=
}

The "..." indicates that I do something pretty particular with the data once I have it in array form. What I wanted to do was display the portion of my solution that would help people in general with a similar need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top