LinqToExcel GetColumnNames at specific Row

本小妞迷上赌 提交于 2019-11-30 16:58:49

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/19359003/linqtoexcel-getcolumnnames-at-specific-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!