Search code examples
c#winformslinqexcellinq-to-excel

Reading columns from excel file using linq


I am reading an excel file but not all the files have the same column headers but the column order are always the same. I want to read the excel file using linq but not specifying specific column names but could i use just columns based on their order?

var _excelFile = new ExcelQueryFactory(openFileDialog1.FileName);
                var _info = from x in _excelFile.Worksheet()
                            select new
                            {

                            };

Solution

  • Yea, you can use the WorksheetNoHeader method to refer to columns by their index rather than their name.

    Here's an example:

    var excel = new ExcelQueryFactory("excelFileName");
    var indianaCompanies = from c in excel.WorksheetNoHeader()
                           where c[2] == "IN" //value in 3rd column
                           select c;