Home » excel » c# – How to get columns from the excel generated using EPPLUS

c# – How to get columns from the excel generated using EPPLUS

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have an excel generated using EPPlus, I want to get all the column names in a sheet. I have tried using LinqToExcel

   var excelFile = new ExcelQueryFactory(excelPath)
   IQueryable<Row> excelSheetValues = from workingSheet in excelFile.Worksheet(sheetName) select workingSheet;
   string[] headerRow = excelFile.GetColumnNames(sheetName).ToArray();

But it is throwing me an error, CAn’t we get the column names of an excel using linq to excel for epplus, as it is working fine while working with Micrsoft Office dlls

How to&Answers:

Try something like this.

var sheet= package.Workbook.Worksheets[1];
foreach (var firstRowCell in sheet.Cells[sheet.Dimension.Start.Row, sheet.Dimension.Start.Column, 1, sheet.Dimension.End.Column]) 
        MessageBox.Show(firstRowCell.Text);

Answer:

@D Rao, for me this worked well

public static string[] GetHeaderColumns(this ExcelWorksheet worksheet)
 {
        List<string> columnNames = new List<string>();
        foreach (var startRowCell in sheet.Cells[worksheet.Dimension.Start.Row, worksheet.Dimension.Start.Column, 1, worksheet.Dimension.End.Column])
            columnNames.Add(startRowCell.Text);
        return columnNames.ToArray();
  }