从datagridview复制到excel时如何保持颜色? [英] How to keep colors when copy-pasting from datagridview to excel?

查看:218
本文介绍了从datagridview复制到excel时如何保持颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下CellFormatting代码来有条件地对我的datagridview中的行进行颜色排列。

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

private void SGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == SGridView.Columns["Name"].Index )
    {
        DataGridViewRow row = SGridView.Rows[e.RowIndex];
        SBomRow BomRow = (SBomRow )row.DataBoundItem;
        switch (BomRow.UsageType())
        {
            case (UsageType.NE):
                break;
            case (UsageType.SV):
                e.CellStyle.BackColor = Color.OrangeRed;
                break;
            case (UsageType.Mix):
                e.CellStyle.BackColor = Color.LightGray;
                break;
            default:
                break;
        }
    }
}

当我将datagridview excel,着色丢失。我知道粘贴时将数据作为HTML和CSV数据添加到剪贴板,因此粘贴到excel时可能不会继续着色。是真的,还是有一种方法来保持颜色的复制粘贴。

When I copy paste from the datagridview to excel, coloring is lost. I know that data is added as HTML and CSV data to the clipboard when pasting, so it is probably impossible to keep coloring when pasting to excel. Is that true, or is there a way to keep colors when copy-pasting.

推荐答案

我相信DataGridView默认存储在剪贴板中,只有标签分隔数据,无格式化

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

但是,您可以使用

使用格式复制功能://www.tcx.be/blog/2005/copy-html-to-clipboard/rel =nofollow> http://www.tcx.be/blog/2005/copy-html-to-clipboard/

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

如果您想提问一个问题,如何处理复制事件来编写一些自定义代码并将格式化的HTML放入剪贴板,我建议使用PreviewKeyDown事件,并写下如

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  
} 

这篇关于从datagridview复制到excel时如何保持颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆