Reputation: 101
Trying to add a List or array to a row in excel with EPPLUS using ExcelRange and LoadFromCollection but is filling a column instead of a a row. code :
List<string> cabeceras = new List<string>();
cabeceras.Add("Fecha");
foreach (ValidationParameterData estacion in informacionSeleccion.Parameters) {
foreach (string parameter in estacion.Parameters) {
cabeceras.Add(estacion.Station + parameter);
cabeceras.Add("L");
}
}
string[] vals = cabeceras.ToArray();
using (ExcelRange rng = ws.Cells[4,1,4,cabeceras.Count])
{
rng.Style.Font.Size = 16;
rng.Style.Font.Bold = true;
rng.LoadFromCollection(vals);//or cabeceras directly
}
tried with both array and list also with:
ExcelRange rng = ws.Cells["A4:M4"]
same thing happens, column A is filled from top to bottom instead of filling the row from left to right, what am I doing wrong? is there another function to do this?
Thanks and regards
Upvotes: 4
Views: 5744
Reputation: 2282
Here is how I solved the problem. See underneath this code for my trials and tribulations re the class Range.
using System;
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;
namespace eeplusPractice
{
class Program
{
static void Main(string[] args)
{
FileInfo newFile = new FileInfo(@"C:\Users\Evan\Desktop\new.xlsx");
ExcelPackage pkg = new ExcelPackage(newFile);
ExcelWorksheet wrksheet = pkg.Workbook.Worksheets[0];
List<string> cabeceras = new List<string>();
for (int x = 1; x < 5; x++)
{
cabeceras.Add("HELLO");
}
int row = 4;
for(int col = 1; col <= cabeceras.Count; col++)
{
int counter = col;
wrksheet.Cells[row, col].Value = cabeceras[counter];
}
pkg.Save();
}
}
}
I tried to iterate through rng object using foreach loop but only one class was able to run and that did the same as the problem you encountered above. It must not treat each cell in range as distinct object that can be accessed. Method
I may write a method LoadRangeHorizontal if I get the time, but for now up top should work OK.
GL lemme know if it works.
Upvotes: 1