Search code examples
c#.netexcelclipboardclipboarddata

How can I properly format text to paste it from C# to Excel?


I have button in my c# application that should format and copy data from few sources.

And i have a problem to paste my text from clipboard to excel.

In excel all text like 1.12.1 is transfromed to 01.12.2001 datetime format! And i dont want to do like that!

foreach (var data in collection)
{
  sb.Append(data.Column1+ "\t");
  sb.Append(data.Column2+ "\t");
  sb.Append(data.Column33+ "\t");

  sb.AppendLine();
}

var clipboardResults = new DataObject();
clipboardResults.SetData(DataFormats.Text, sb.ToString());
Clipboard.SetDataObject(clipboardResults);

Is it possible to format my text so excel will understand that copied data is text and not date time!


Solution

  • Did you try putting the text identifier (single quote) in front of it?

    e.g. '1.12.1