سؤال

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.

هل كانت مفيدة؟

المحلول

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());

نصائح أخرى

An FYI for future googlers:

It appears that GetColumnNames() has changed since the above answer was accepted.

There is now an overload in which you can define the range of the header row as a string:

 // This will return a List<string> 
 var colNames = ExcelFile
                .GetColumnNames(SheetName, "A9:AF9")
                .ToList();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top