Excel.Range Copy() works very slowly

2019-07-29 06:43发布

I'm building a report in which try to copy the previous row cell styles in the following

for (int i = 0; i < DataSource.Length; i++)
{
    int rowNumber = i + s;
    Excel.Range RngToCopy = ObjWorkSheet.get_Range("A" + rowNumber.ToString(), "K" + rowNumber.ToString());
    Excel.Range r = ObjWorkSheet.get_Range("A" + (rowNumber + 1).ToString(), "K" + (rowNumber + 1).ToString());
    RngToCopy.Copy(Type.Missing);
    r.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
    r.PasteSpecial(Excel.XlPasteType.xlPasteFormats,
                   Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
    ObjWorkSheet.Cells[rowNumber, 1] = i + 1;
    ObjWorkSheet.Cells[rowNumber, 2] = DataSource[i].TerminalName;
    ObjWorkSheet.Cells[rowNumber, 3] = DataSource[i].Type;
    ObjWorkSheet.Cells[rowNumber, 4] = DataSource[i].Requisite;

}

everything works but very long How can I speed this up?

1条回答
混吃等死
2楼-- · 2019-07-29 07:11

I think you manually copy you will be improved.

Object[] origData = origRange.Value2;
destRange.Value2 = origData;

That should be WAY faster.

查看更多
登录 后发表回答