How to set a collection of objects onto a row, one object per cell?

こ雲淡風輕ζ 提交于 2019-12-02 06:58:21

问题


With EPPlus, given a collection of objects, how do I set the objects on a row, one object per cell?

That is, given:

int rowNumber = ...
int columnNumber = ...
IEnumerable<object> values = ...

How to set the values on the row rowNumber, starting on the column columnNumber, one value per cell?


回答1:


Use the LoadFromArrays method on a range. The first cell where the arrays values should be set on the row is used to create the range.

The argument passed to LoadFromArrays is an IEnumerable of object arrays, so the values to set on the row should be first converted to an object[] and then wrapped as an IEnumerable

int rowNumber = ...
int columnNumber = ...
IEnumerable<object> values = ...

var range = ws.Cells[rowNumber, columnNumber];
range.LoadFromArrays(AsEnumerable(values.ToArray())); 

/* ... */

// see http://stackoverflow.com/q/1577822/614800 for a discussion on how
// to wrap an object into an IEnumerable
private static IEnumerable<T> AsEnumerable<T>(T obj)
{
    yield return obj;
}


来源:https://stackoverflow.com/questions/21118651/how-to-set-a-collection-of-objects-onto-a-row-one-object-per-cell

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