简体   繁体   English

如何格式化导出到Excel的DataGridView的背景?

[英]How to format background of DataGridView exported to Excel?

I want to format the background of my header cells in excel, which is populated trhough my Datagridview. 我想在Excel中格式化标题单元格的背景,该背景是通过Datagridview填充的。 I´ve tried it with 我尝试过

ws.Cells[1. 2].Style.Fill.PatternType = ExcelFillStyle.Solid;

ws.Cells[1, 2].Style.Fill.BackgroundColor.SetColor(Color.LightGray);

but i got a error message which says the name ExcelFillStyle.Solid does not exist in the current context. 但我收到一条错误消息,提示名称ExcelFillStyle.Solid在当前上下文中不存在。 Any ideas? 有任何想法吗?

Thank you 谢谢

Firstly welcome to the SO community as a new contributor ! 首先, 欢迎 SO社区成为新的贡献者

Doing some research I would recommend changing the header-color of the GridView using something like this: 做一些研究,我建议使用以下方法更改GridView标题颜色

_dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
_dataGridView.EnableHeadersVisualStyles = false;

This was found on SO here: SO - How to change the color of dataGridView header 这是在SO上找到的: SO-如何更改dataGridView标头的颜色

However If you wanted to tackle this using a more custom approach where you can set specific cells (items) background colors, this can be done using using: 但是,如果您想使用一种更自定义的方法来解决此问题,即可以设置特定单元格 (项目)的背景颜色,则可以使用以下方法完成:

DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)

columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;

The use case for this is explained on SO here: SO上对此用例进行了说明:

Focus-a-specific-cell 焦点一特定的细胞

Cell-color-changing-in-excel-using-c-sharp 细胞变色功能于Excel的使用-C-尖锐

Using these two sources should help you achieve what you need as these both have been answered . 使用这两个资源应该可以帮助您实现所需的目标,因为这两个问题都已得到解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM