Change excel sheet cell color through VB.NET

I am writing some data from a database to excel through visual basic.net. I need to change the background of some cells, as well as make the text bold. I need something like this:

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

Of course, none of the above works. How can i achieve this? Thank..

+5
source share
5 answers

You need to create an Excel.Style object and apply it to the range. Like this:

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"
+9
source

It worked perfectly for me.

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

+4
source

, 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
+2
    void SetCaptionStyle(ExcelStyle style)
    {
        style.Fill.PatternType = ExcelFillStyle.Solid;
        style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));

    }
0

:

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

All Articles