【问题标题】:ExcelDna/NetOffice/Excel: Most efficient way to insert values? [duplicate]ExcelDna/NetOffice/Excel:插入值的最有效方法? [复制]
【发布时间】:2015-02-01 09:54:09
【问题描述】:

我正在使用 ExcelDna 和 NetOffice 为 Excel 构建一个插件,并从各种来源获取数据并将其插入到工作表中。

我的问题是在工作表中插入数据真的很慢。

向 Excel 中插入值块最有效的方法是什么?

ResultCell[,] result = _currentView.GetResult(values, cursor);

int loopM = result.GetUpperBound(0);
int loopN = result.GetUpperBound(1);

for (int y = 0; y <= loopM; y++)
{
    for (int x = 0; x <= loopN; x++)
    {
        int a = xlCurrentCell.Row + row;
        int b = xlCurrentCell.Column + x;
        Excel.Range xlCell = (Excel.Range)xlActiveSheet.Cells[a, b];
        _SetValue(xlCell, result[y, x]);
    }
    row++;
}

...

private void _SetValue(Excel.Range xlCell, ResultCell cell)
{
    xlCell.ClearFormats();
    object value = cell.Value;

    if (value is ExcelError)
    {
        value = ExcelUtils.ToComError((ExcelError) value);
    }
    else if (ExcelUtils.IsNullOrEmpty(value))
    {
        value = "";
    }

    xlCell.Value = value;
    if (!string.IsNullOrEmpty(cell.NumberFormat))
    {
        xlCell.NumberFormat = cell.NumberFormat;
    }
}

问题是每次插入都会变成屏幕更新吗?我试过了:

Excel.Application xlApp = new Excel.Application(null, ExcelDna.Integration.ExcelDnaUtil.Application);

xlApp.ScreenUpdating = false;

但这没有任何效果。

【问题讨论】:

    标签: c# excel excel-dna netoffice


    【解决方案1】:

    为每个单元格设置数据很慢。您可以将大范围的值设置为一个值数组,这将大大加快。

    暂时关闭屏幕更新和重新计算也会有所帮助。

    有关冗长而详细的讨论,包括使用 Excel-DNA 插件中的 C API 的代码,请参阅Fastest way to interface between live (unsaved) Excel data and C# objects

    【讨论】:

    • 谢谢。这就是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2016-03-04
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多