0

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
                            {

                            };
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
Masriyah
  • 2,445
  • 11
  • 49
  • 91

1 Answers1

1

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;
Paul
  • 18,349
  • 7
  • 49
  • 56
  • So if i have 13 columns i would do c[1] .... all the way to c[13]. what does "IN" stand for? am i supposed to replaced with an item in a column? – Masriyah Aug 26 '13 at 16:27
  • I removed the where statement and created a foreach statement for each item in "indianaCompanies" and assigned a header based on the column: ElementAt(0)... – Masriyah Aug 26 '13 at 18:46
  • The code is just an example. So in this case it is filtering to only include rows whose value at the second index equals 'IN' – Paul Aug 26 '13 at 20:35
  • 1
    @Masriyah It's a 0 based index, so if you have 13 columns then it would be between c[0] and c[12] – Paul Aug 26 '13 at 20:35