批量获取Excel单元格属性
当从 Excel 中的某个范围中获取值时,“批量”获取值(作为二维数组)比循环遍历每一行和每一列要高效得多。例如:
Dim range = Globals.Table.Range("A1:E5")
Dim values(,) As Object = range.Value
对于 25 个单元格来说,差别不大,但是对于 10,000 行 x 20 列来说,差别肯定很大。到目前为止,一切都很好。
我的问题是:如何“批量”获取其他属性?
例如,我想找出哪些单元格以某种方式着色。我想做一些类似“range.Interior.Color”的事情,但它只返回一个值,而不是值数组。所以我最终会进行循环,这可能会慢 100 倍甚至 1000 倍。对于大桌子来说,这确实是一个杀手。
看起来 .Formula
的行为就像 .Value
一样:我可以一次获取多个。但我还没有说服颜色发挥得很好。
When fetching values from a range in Excel, it is much more efficient to get the values in "bulk" (as a 2D array) than looping through each row and column. For example:
Dim range = Globals.Table.Range("A1:E5")
Dim values(,) As Object = range.Value
With 25 cells it doesn't make much of a difference, but with 10,000 rows by 20 columns it certainly does. So far so good.
My question is: How can one do "bulk" fetching for other properties?
For example, I want to find which cells are colored a certain way. I would love to do something like "range.Interior.Color", but that returns just one value, not an array of values. And so I end up looping, which is probably 100 or even 1000 times slower. For large tables, this is really a killer.
It looks like .Formula
behaves just like .Value
: I can fetch multiple ones at a time. But I've yet to cajole colors into playing nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您无法以数组形式获取这些属性,因为 Excel 存储这些信息的方式不同。 Excel 不会单独存储每个单元格的格式,而是存储特定的格式混合以及使用该格式的范围的内部“列表”。
您可以通过创建一个具有各种格式的小测试文件并将其保存为 XML 格式(至少在 2010 年,您需要使用“XML Spreadsheet 2003”)来了解格式的存储方式。
这篇文章也可能帮助。
I don't think you can get those properties as an array because of how Excel stores that information. Excel does not store formatting for each cell individually but rather stores a particular blend of formats along with an internal "list" of the ranges that use that format.
You can get a sense of how the formatting is stored by creating a small test file with various formats and saving it as XML format (in 2010 at least, you need to use "XML Spreadsheet 2003").
This article may also help.
“我想查找哪些单元格以某种方式着色”
在 VBA 中,您可以使用按格式搜索的
Find
方法运行快速例程。例如,查找与 A1 中的单元格具有相同单元格字体颜色和内部颜色的所有单元格。我想你可以在 VSTO 中使用类似的东西"I want to find which cells are colored a certain way"
in VBA you can run a quick routine using the
Find
method that searches by format. For example to find all cells with the same cell font colour and interior colour as the cell in A1. I presume you can use something similar in VSTO