通过 VB.NET 更改 Excel 表格的单元格颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6983141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:38:21  来源:igfitidea点击:

Changing cell color of excel sheet via VB.NET

vb.netexcel

提问by dnur

I am writing some data from database to the excel via visual basic.net.I need to change background of some cells and also need to make text bold. I need something like that :

我正在通过visual basic.net将一些数据从数据库写入excel。我需要更改某些单元格的背景,还需要将文本加粗。我需要这样的东西:

 xlWorkSheet.Cells(rownumber, 1).BackgroundColor = Color.Yellow
 xlWorkSheet.Cells(rownumber, 1).Font.isBold = True

Of course none of above is works.How can I achieve this? Thanks..

当然,以上都不起作用。我怎样才能做到这一点?谢谢..

回答by Edwin de Koning

You need to create a Excel.Style object, and apply that to a range. Like this:

您需要创建一个 Excel.Style 对象,并将其应用于一个范围。像这样:

Dim style As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")
style.Font.Bold = True
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)

xlWorkSheet.Cells(rownumber, 1).Style = "NewStyle"

回答by Jason Rule

This worked perfect for me.

这对我来说很完美。

xlsWorkSheet.Cells(row, column).interior.color = Color.Green

xlsWorkSheet.Cells(row, column).interior.color = Color.Green

回答by Emmanuel López

Thats few declaration that can help you for style an Excel
For color palette: http://dmcritchie.mvps.org/excel/colors.htm

很少有声明可以帮助您设计 Excel
For 调色板的样式:http: //dmcritchie.mvps.org/excel/colors.htm

Dim xlsCell As Excel.Range
xlsCell = xlWorkSheet.Range("A1")
xlsCell.Range("A5").Value = "TEXT"

With xlsCell.Range("A12:J12")
    .Merge()
    .Borders(XlBordersIndex.xlEdgeBottom).Weight = 2
    .Borders(XlBordersIndex.xlEdgeTop).Weight = 2
    .Borders(XlBordersIndex.xlEdgeLeft).Weight = 2
    .Borders(XlBordersIndex.xlEdgeRight).Weight = 2
    .Borders(XlBordersIndex.xlInsideHorizontal).Weight = 2
    .Borders(XlBordersIndex.xlInsideVertical).Weight = 2
    .Interior.ColorIndex = 15                        
    .WrapText = True
    .Font.Name = "Arial"
    .VerticalAlignment = Excel.XlHAlign.xlHAlignCenter
    .HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft
End With

回答by Jagdeep

    void SetCaptionStyle(ExcelStyle style)
    {
        style.Fill.PatternType = ExcelFillStyle.Solid;
        style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));

    }

回答by F0r3v3r-A-N00b

This worked for me:

这对我有用:

oWorkSheet.Range(oWorkSheet.Cells(nRow, 1), oWorkSheet.Cells(nRow, 5)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.DimGray)

回答by user12885413

Dim style as:

昏暗的风格为:

Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")
style.Font.Bold = True
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)

xlWorkSheet.Cells(rownumber, 1).Style = "NewStyle"