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

时间:2011-08-08 13:51:14

标签: vb.net excel

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

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

当然以上都不是有效的。我怎么能做到这一点?感谢..

6 个答案:

答案 0 :(得分:9)

您需要创建一个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"

答案 1 :(得分:4)

这对我来说非常适合。

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

答案 2 :(得分:2)

这很少有声明可以帮助你的风格Excel 对于调色板: 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

答案 3 :(得分:0)

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

    }

答案 4 :(得分:0)

这对我有用:

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

答案 5 :(得分:0)

昏暗风格为:

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"