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?
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