Valip
Valip

Reputation: 4630

C# Excel Interop set multiple cell Values at once

How can I add values to 5 cells on the same row at once?

This is how add them right now:

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";

Expected result:
enter image description here

Upvotes: 3

Views: 1449

Answers (1)

R3uK
R3uK

Reputation: 14537

Use a range and an array :

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

Upvotes: 4

Related Questions