How to keep colors when copy-pasting from datagridview to excel?

前端 未结 2 818
我寻月下人不归
我寻月下人不归 2021-01-07 10:47

I use the following CellFormatting code to conditionally color rows in my datagridview.

private void SGridView_CellFormatting(object sender, DataGridViewCel         


        
相关标签:
2条回答
  • 2021-01-07 11:40

    I believe that DataGridView by default stores in the clipboard only tab delimited data without formatting

    But you can write your custom Copy with Formatting function using the method, described in

    http://www.tcx.be/blog/2005/copy-html-to-clipboard/

    Just in case if you will want to ask a question how to handle copy event to write some custom code and put formatted HTML to clipboard, I would suggest using PreviewKeyDown event and write something like

    if (e.KeyData.ToString() == "C, Control") {  your formatting code goes here  
    } 
    
    0 讨论(0)
  • 2021-01-07 11:41

    I was able to set the colors by using a modified version of the code found here. First I changed the CopyHtmlToClipBoard() to a public static void method and place it in an Extension class. Next, when calling the code, I set a var to capture each cells color and then modified the html.AppendFormat()statement to include the color being passed. Here is a copy of the code used:

     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(
              begin
              , 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 MemoryStream(
              enc.GetBytes(html_total)));
    
            Clipboard.SetDataObject(obj, true);
        }
    
    
         private void copyAlltoClipboard()
        {
    
            var DataGridView1Counts = DataGridView1.Rows.Count;
    
    
            StringBuilder html = new StringBuilder();
            html.Append("<table>");
    
            if (DataGridView1Counts > 0)
            {
                //sets headers
                html.Append("<tr>");
                html.Append("<th> Name1 </th>");
                html.Append("<th> Name2 </th>");
                html.Append("<th> Name3 </th>");
                html.Append("<th> Name4 </th>");
                html.Append("<th> Name5 </th>");
    
                foreach (DataGridViewRow row in DataGridView1.Rows)
                {
                    html.Append("<tr>");
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        var cellcolor = cell.Style.BackColor;
                        html.AppendFormat("<td bgcolor = " + cellcolor.Name + ">{0}</td>", cell.Value);
                    }
                    html.Append("</tr>");
                }
            }
            html.Append("</table>");
            Extensions.CopyHtmlToClipBoard(html.ToString());
        }
    
    0 讨论(0)
提交回复
热议问题