开发者

Changing cell color of excel sheet via VB.NET

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 :

 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..


You need to create a Excel.Style object, and apply that to a 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"


This worked perfect for me.

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


Thats few declaration that can help you for style an Excel
For color palette: 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


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

    }


This worked for me:

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


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜