c# datagridview - keep formatting on copy/paste into excel

Posted on 2014-08-15
Last Modified: 2014-09-13
hi all

I have a c# 2010 winforms  application which displays data in a datagridview. The table is formatted using colours and bold font etc etc.

When it is copied and pasted into excel the formatting is lost.

is there any way to keep the formatting on paste into excel?

Question by:cycledude
    LVL 96

    Expert Comment

    by:Bob Learned
    You should use HTML, and set the clipboard format as HTML, and then it should keep the style from the DataGridView.

    Author Comment

    Sounds interesting

    how do you use html in the datagridview?
    LVL 96

    Expert Comment

    by:Bob Learned
    Take a look here:

    Copying an HTML-fragment to the Clipboard

    using System;
    using System.Text;
    using System.Windows.Forms;
    class Test {
    public static void CopyHtmlToClipBoard(string html) {
       Encoding enc = Encoding.UTF8;
       string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
          + "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";
       string html_begin = "<html>\r\n<head>\r\n"
          + "<meta http-equiv=\"Content-Type\""
          + " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
          + "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
          + "<!--StartFragment-->";
       string html_end = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";
       string begin_sample = String.Format(begin, 0, 0, 0, 0);
       int count_begin = enc.GetByteCount(begin_sample);
       int count_html_begin = enc.GetByteCount(html_begin);
       int count_html = enc.GetByteCount(html);
       int count_html_end = enc.GetByteCount(html_end);
       string html_total = String.Format(
          , count_begin
          , count_begin + count_html_begin + count_html + count_html_end
          , count_begin + count_html_begin
          , count_begin + count_html_begin + count_html
          ) + html_begin + html + html_end;
       DataObject obj = new DataObject();
       obj.SetData(DataFormats.Html, new System.IO.MemoryStream(
       Clipboard.SetDataObject(obj, true);

    Open in new window


    Author Comment


    How do you enter the html into the datagrid?

    Accepted Solution

    The way I got around this, was to paste special into excel, this kept the formatting of the existing spreadsheet, thus allowing the colours etc to remain intact in the spreadsheet...

    bit of a cop out but it was the only way I could get the colour information

    Author Closing Comment

    could not find another solution

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now