简体   繁体   中英

Epplus add values to a range of cells

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?

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:

在此处输入图片说明

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 });

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM