Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Epplus add values to a range of cells

Tags:

c#

excel

epplus

In C#, using Microsoft.Office.Interop.Excel , you can assign values to an entire row at once like this:

string[] headings = { "Value1", "Value2", "Value3" };
excelSheet.get_Range("A1:C1", Type.Missing).Value2 = headings;

Is there any similar functionality provided in the EPPlus library?

like image 668
R. Gruber Avatar asked Dec 25 '22 10:12

R. Gruber


2 Answers

You could use the .LoadFromCollection method.

So, for example:

var vals = new string[] { "value1", "value2", "value3" };
var rng = sheet.Cells["A1:A3"];

rng.LoadFromCollection(vals);

will produce this output:

enter image description here

like image 177
Stewart_R Avatar answered Dec 27 '22 03:12

Stewart_R


A little clunky but here is an example.

// A row of numbers
var numbers = new List<object> {1.1, 2.2, 3.3};
var horizontalRange = sheet.Cells[$"A1:A{numbers.Count}"];
horizontalRange.LoadFromArrays(new List<object[]> {numbers.ToArray()});

// a grid
var colours = new object[] { "red", "green", "blue"};
var range2d = sheet.Cells["A3:C4"];
range2d.LoadFromArrays(new List<object[]> { numbers.ToArray(), colours });

enter image description here

like image 40
David Hollinshead Avatar answered Dec 27 '22 03:12

David Hollinshead