0

Which would be potentially a best way to enumerate or iterate or simply look for empty cells or cells with specific data structure in Excel, and later once you find it do some processing on it.

I tired Range, Value, Value2, etc but it takes fairly long time when Excel Sheet is considerably larger. I believe there must be some other efficient way. It would be nice, if you can show some example snippet.

4

2 回答 2

1

要查找空白单元格,请使用.SpecialCells范围对象的方法。

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.specialcells(v=office.11​​).aspx

.specialCells方法返回匹配条件的范围对象(即xlCellTypeVisiblexlCellTypeBlanks等)。然后,您可以迭代此范围以执行格式化等。

更新我不是 C# 程序员,但我可以向您展示如何在 VBA 中执行此操作。假设互操作公开了大多数/所有相同的方法和功能,您应该希望能够根据您的目的进行翻译。

Sub ColorVisibles()
Dim rng As Range
Dim rngBlanks As Range
Dim blanksExist As Boolean

'define your range
Set rng = Range("A1:AA300")

'check to make sure there are blank cells in the range:
blanksExist = Application.WorksheetFunction.CountBlank(rng) > 0

If blanksExist Then
    Set rngBlanks = rng.SpecialCells(xlCellTypeBlanks)
    rngBlanks.Interior.Color = vbYellow

Else:
    MsgBox "No blank cells exist in the specified range.", vbInformation
End If

End Sub
于 2013-05-17T13:01:23.347 回答
1

答案相对简单:从 excel 中批量获取数组(搜索 SO 以获取操作方法) - 测试空单元格的 erray 值,然后仅访问 excel 中的空单元格。

这有点麻烦,但速度更快,因为迭代每个单元格比简单地批量获取所有数据要慢得多。

于 2013-05-17T10:45:47.263 回答