简体   繁体   English

Epplus将值添加到一系列单元格中

[英]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: 在C#中,使用Microsoft.Office.Interop.Excel ,您可以一次将值分配给整个行,如下所示:

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

Is there any similar functionality provided in the EPPlus library? EPPlus库中是否提供任何类似的功能?

You could use the .LoadFromCollection method. 您可以使用.LoadFromCollection方法。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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