【问题标题】:How to format background of DataGridView exported to Excel?如何格式化导出到 Excel 的 DataGridView 的背景?
【发布时间】:2019-06-04 03:37:31
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: c# excel winforms


    【解决方案1】:

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

    做一些研究,我建议使用以下方法更改 GridViewheader-color

    _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解释了这个用例:

    Focus-a-specific-cell

    Cell-color-changing-in-excel-using-c-sharp

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

    【讨论】:

    • 我认为它只适用于 ExcelFillStyle.Solid 但它适用于 columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;还。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多