我编写了一个 VSTO DLL 来对用户指定的单元格范围执行转换。我正在按以下方式执行此操作:
Globals.ThisAddIn.Application.EnableEvents = false;
int totRows=inputRange.Rows.Count;
int totCols=inputRange.Columns.Count;
for (int i = 1; i <= totRows; i++) {
for (int j = 1; j <= totCols; j++) {
if (((Range)inputRange.Cells[i, j]).Value2 != null) {
((Range)outputSheet.Cells[i, j]).Value2 = MyTransform(((Range)inputRange.Cells[i, j]).Value2);
}
}
}
Globals.ThisAddIn.Application.EnableEvents = true;
这种方法允许我修改大约 150 万个单元格/小时(我没有计算 MyTransform() 消耗的时间),这在我看来非常慢。
在 VSTO 中是否有更有效的读/写单元格的方法?
如果不是,将其转换为 XLL(可能使用 Excel-DNA)会更快吗?猜猜多少钱?
请您参考如下方法:
您正在为您正在读取和设置的每个单元格进行多个 COM 调用。一次将整个范围作为数组处理会更有效率。
这个问题对选项进行了广泛的讨论:Fastest way to interface between live (unsaved) Excel data and C# objects
与 Excel-DNA和 Excel C API,你每秒可以读写一百万个单元格,但我猜即使通过 COM,如果你读写一个大数组,性能也会非常好。
