I want to add header column using a List.
I tried with this, but it print only first column.
List<string> headerColumns1 = new List<string>()
{
"1",
"2",
"3"
};
ExcelPackage excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
workSheet.Cells[1, 1].LoadFromCollection(headerColumns, false); //
workSheet.Cells[2, 1].LoadFromCollection(data, false);
it prints only "1" in first column.
How can I print the all header columns in first row?
You specified only one Cell
This code: workSheet.Cells[1, 1]
shows your mistake
You have two options: Specify the range
using (ExcelRange range = ec.Cells[1, 1, 1, headerColumns.Count])
{
range.LoadFromCollection(headerColumns, false);
}
Or use foreach to populate manually
int index = 1;
foreach(var item in headerColumns)
{
workSheet.Cells[1, index++].Value = item;
}
Take a look to LoadFromCollection
Load a collection into a the worksheet starting from the top left row of the range.
p.s. I believe it's just typing error: List<string> headerColumns1 = new List<string>()
against workSheet.Cells[1, 1].LoadFromCollection(headerColumns, false);