Epplus add values to a range of cells

随声附和 提交于 2019-12-12 12:32:07

问题


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?


回答1:


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:




回答2:


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



来源:https://stackoverflow.com/questions/34882275/epplus-add-values-to-a-range-of-cells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!