문제

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