如何使用vb.net为excel中的单元格着色?

如何使用vb.net为excel中的单元格着色?,vb.net,excel,Vb.net,Excel,如何对单元格进行垂直着色?示例我希望将单元格“I”中的所有数据着色到最后一个 以下是我迄今为止所做的尝试: For i = 0 To ds.Tables(0).Rows.Count - 1 For j = 0 To ds.Tables(0).Columns.Count - 1 xlSheet.Cells(i + 8, j + 1) = ds.Tables(0).Rows(i).Item(j)

如何对单元格进行垂直着色?示例我希望将单元格“I”中的所有数据着色到最后一个

以下是我迄今为止所做的尝试:

For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                xlSheet.Cells(i + 8, j + 1) =
                ds.Tables(0).Rows(i).Item(j)
                formatRange = xlSheet.Cells(i + 8, j + 1)
                formatRange.BorderAround(Excel.XlLineStyle.xlDash, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic)
                formatRange = xlSheet.Cells(i + 8, 9 + j)
                formatRange.Interior.Color = system.Drawing.ColorTranslator.ToOle(system.Drawing.Color.Red)
            Next
        Next

这不适合你吗:

xlSheet.Range("I:I").Interior.Color = System.Drawing.ColorTranslator.ToOle(system.Drawing.Color.Red)

这会帮你的

Sub ColorCol(ColID As String, R As Long, G As Long, B As Long)
Dim LastRow As Long
Dim Rr As Range
With ThisWorkbook.ActiveSheet
LastRow = .Cells(.Rows.Count, ColID).End(xlUp).Row
Set Rr = .Range(ColID & "1", ColID & LastRow)
For Each Cell In Rr.Cells
Cell.Interior.Color = RGB(R, G, B)
Next
End With
End Sub
Sub Main()
ColorCol "A", 255, 0, 0
End Sub
从另一艘潜艇呼叫

Sub Main()
ColorCol "A",255,0,0
End Sub

备注:在您的工作表上工作

否,先生。“我想要单元格中的所有数据,我有一种颜色。@米格斯:哪个单元格?你说的是单元格“I”,在Excel中没有任何意义,所以我猜想你实际上指的是列“I”。哦,天哪,我很难用英语解释。我可以在这里上传照片吗?@Miggs我的回答对你有帮助吗?