Set cell value using Excel interop

后端 未结 4 659
孤街浪徒
孤街浪徒 2020-12-17 10:20

Ok, so I\'m trying to set the value of a cell with the excel interop library. I am able to do it with the following:

sheet.Cells[row, col] = value;
<         


        
4条回答
  •  青春惊慌失措
    2020-12-17 10:51

    Have you tried setting all of the values at once, rather than iterating through your array and setting one cell at a time? That way you only have to pass data over the COM boundary once, rather than once per cell.

    Excel is very flexible in this regard. Try the following:

    int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    Range rng = excelApp.get_Range("A1", "J1");
    rng.Value = intArray; 
    

    You should this faster than iterating over each of the cells you're wanting to set.

    Other than that, turn off ScreenUpdated as Andy suggests and also consider setting calculation to manual until you've finished your copy process.

提交回复
热议问题