Microsoft Excel外接程序性能问题

本文关键字:程序性 性能 问题 程序 Excel Microsoft | 更新日期: 2023-09-27 18:05:56

进一步的代码在.xls中工作得或多或少不错,但在.xlsx中却相当慢。问题在哪里?

vector = activeSheet.Columns["A", Type.Missing];
double[] temp = new double[vector.Rows.Count];
int i = 0;
foreach (var item in vector.Value2)
{
    if (item == null)
        break;
    temp[i] = vector.Value2[i + 1, 1];
    i++;
}
array = new double[i];
for (int j = 0; j < i; j++)
{
    array[j] = temp[j];
}

Microsoft Excel外接程序性能问题

一行有问题:

vector = activeSheet.Columns["A", Type.Missing];

我把上面的代码改成:

double[] temp = new double[activeSheet.Rows.Count];
int i = 0;
for (i = 0; i < activeSheet.Rows.Count; i++)
{
    var item = (activeSheet.Cells[i + 1, 1] as Excel.Range).Value2;
    if (item == null)
        break;
    temp[i] = item;
}
...

现在它在眨眼间就起作用了。谢谢@Sayse和@Eddy的建议。