C#Excel Interop一次设置多个单元格值

时间:2017-03-08 11:17:15

标签: c# excel interop

如何一次在同一行的5个单元格中添加值?

这就是现在添加它们的方法:

ws.Cells[1, 1] = "1";
ws.Cells[1, 2] = "2";
ws.Cells[1, 3] = "3";
ws.Cells[1, 4] = "4";
ws.Cells[1, 5] = "5";

预期结果:
enter image description here

1 个答案:

答案 0 :(得分:3)

使用范围和数组:

var rng = (Excel.Range)ws.Range[ws.Cells[1, 1], ws.Cells[1, 5]];
rng.Value = new int[] { 1, 2, 3, 4, 5 }; // one dimensional array