简体   繁体   English

最快的方式来枚举或查找所有空的Excel单元格并更改其样式或执行其他处理

[英]Fastest way to enumerate or look for all empty Excel cells and change their style or do some other processing

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. 这可能是枚举或迭代的最佳方式,或者只是在Excel中查找具有特定数据结构的空单元格或单元格,稍后当您发现它对其进行一些处理时。

I tired Range, Value, Value2, etc but it takes fairly long time when Excel Sheet is considerably larger. 我厌倦了Range,Value,Value2等,但Excel Sheet相当大的时候需要相当长的时间。 I believe there must be some other efficient way. 我相信必须有其他一些有效的方法。 It would be nice, if you can show some example snippet. 如果你能展示一些示例片段,那就太好了。

The answer is relativley simple: get the array in one batch from excel (search SO for a how to) - test the values of the erray for empty cells and then acess only the empty cells in excel. 答案是相对简单的:从excel中获取一批数组(搜索SO以获取方法) - 测试空单元格的erray值,然后只访问excel中的空单元格。

It is somewhat cumbersome, but the fastes way because iterating each cell is vastly slower than simply getting all data in a batch. 这有点麻烦,但是因为迭代每个单元格比简单地批量获取所有数据要慢得多。

To find blank cells, use the .SpecialCells method of a range object. 要查找空白单元格,请使用范围对象的.SpecialCells方法。

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

The .specialCells method returns a range object of the matching criteria (ie, xlCellTypeVisible , xlCellTypeBlanks , etc.). .specialCells方法返回匹配条件的范围对象(即xlCellTypeVisiblexlCellTypeBlanks等)。 You can then iterate of this range to perform your formatting, etc. 然后,您可以迭代此范围以执行格式化等。

Update I'm not a C# programmer, but I can show you how I would do this in VBA. 更新我不是C#程序员,但我可以告诉你如何在VBA中这样做。 Assuming interop exposes most/all of the same methods and functionality, you should hopefully be able to translate this for your purposes. 假设interop暴露了大部分/全部相同的方法和功能,您应该希望能够为您的目的翻译它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM