【问题标题】:Getting the colour of Excel cells using interop使用互操作获取 Excel 单元格的颜色
【发布时间】:2010-05-11 19:27:31
【问题描述】:

我有一系列从 excel 检索的单元格,我想知道如何获取列中单元格的颜色(所有单元格都是不同的颜色)。到目前为止,我有:

Range range = sheet.get_Range( "A1", "D10" );

这将我需要的数据放入对象数组中,但我希望能够遍历行并获取“A”列的单元格颜色。这可能吗?

我知道这是可能的:

Range range = sheet.get_Range( "A1", Missing.Value );
var colour = range.Interior.Color;

但我不想对每个单独的单元格都这样做。

谢谢

【问题讨论】:

    标签: .net excel interop


    【解决方案1】:

    每个 Range 都有一个返回 Range 的 Cells 和 Rows 属性。此外,Cells 返回一个 Range。以下是一些示例。

        Dim oRange As Excel.Range = CType(Me.Application.ActiveSheet, Excel.Range).Range("A1", "D10")
    
        For Each oRowRange As Excel.Range In oRange.Rows
            For Each oCellRange As Excel.Range In oRowRange.Columns
                Debug.WriteLine(oCellRange.Interior.Color)
            Next
        Next
    
        For Each oRowRange As Excel.Range In oRange.Range("A1")
            Dim oCell As Excel.Range = CType(oRowRange.Cells(RowIndex:=1, ColumnIndex:=1), Excel.Range)
        Next
    
        For i As Integer = 1 To oRange.Rows.Count
            Dim oCell As Excel.Range = CType(oRange.Cells(RowIndex:=i, ColumnIndex:=1), Excel.Range)
        Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多