3

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

D Rao
  • 41
  • 2
  • 8

2 Answers2

1

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);
MatthewD
  • 6,719
  • 5
  • 22
  • 41
1

@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();
  }
Arun Banakar
  • 244
  • 1
  • 7