Excel.Range Copy() works very slowly

你说的曾经没有我的故事 提交于 2019-12-24 14:29:45

问题


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:


I think you manually copy you will be improved.

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

That should be WAY faster.



来源:https://stackoverflow.com/questions/24029822/excel-range-copy-works-very-slowly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!