通过VB.NET改变excel表格的单元格颜色

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

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

当然以上都不行,我该怎么办? 谢谢..

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

这对我来说非常合适。

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

这就是几个可以帮你打造一个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.FontStyle = "Arial" .VerticalAlignment = Excel.XlHAlign.xlHAlignCenter .HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft End With