LinqToExcel GetColumnNames at specific Row

Carlos Novo picture Carlos Novo · Oct 14, 2013 · Viewed 7.6k times · Source

I am using the library LinqToExcel to read excel files in my mvc4 project. My problem is when I try to read the headers at row 4... How I can do this?

In project, exists a function that returns all the column names, but I suppose that the columns need to be at row 0.

    // Summary:
    //     Returns a list of columns names that a worksheet contains
    //
    // Parameters:
    //   worksheetName:
    //     Worksheet name to get the list of column names from
    public IEnumerable<string> GetColumnNames(string worksheetName);

Thanks.

Answer

Paul picture Paul · Oct 14, 2013

Unfortunately the GetColumnNames() method only works when the header row is on row 1.

However, it should be possible to get the column names by using the WorksheetRangeNoHeader() method.

It would look something like this

var excel = new ExcelQueryFactory("excelFileName");
// Only select the header row
var headerRow = from c in excel.WorksheetRangeNoHeader("A4", "Z4")
                select c;

var columnNames = new List<string>();
foreach (var headerCell in headerRow)
  columnNames.Add(headerCell.ToString());