首页 > 解决方案 > 如何格式化导出到 Excel 的 DataGridView 的背景?

问题描述

我想在 excel 中格式化我的标题单元格的背景,它是通过我的 Datagridview 填充的。我试过了

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

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

但我收到一条错误消息,显示名称 ExcelFillStyle.Solid 在当前上下文中不存在。有任何想法吗?

谢谢

标签: c#excelwinforms

解决方案


首先欢迎作为新的贡献者加入 SO 社区!

做一些研究,我建议使用以下内容更改GridView标题颜色

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

这是在SO上找到的: SO - How to change the color of dataGridView header

但是,如果您想使用更自定义的方法来解决这个问题,您可以设置特定的单元格(项目)背景颜色,这可以使用以下方法完成:

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

columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;

此处的SO对此用例进行了解释:

聚焦特定细胞

使用 c-sharp 在 excel 中更改单元格颜色

使用这两个来源应该可以帮助您实现您所需要的,因为这两个来源都已得到解答


推荐阅读