Search code examples
linqopenxmlopenxml-sdklinq-to-excel

Read first row of excel sheet using OpenXML and LINQ


I want to read first row of the excel work sheet using Open XML sdk with LINQ. Is there any way I could get it with out using following code:

WorkSheetPart.Worksheet.Descendants<Row>().FirstOrDefault();

The above code is taking long time (almost 1 minute) to retun the first row as the sheet contains morethan 75000 rows.


Solution

  • You could use LinqToExcel to read the first row, and it should be faster because it doesn't have to load the whole file. However, LinqToExcel uses OLEDB to read the file instead of the Open XML SDK.

    var excel = new ExcelQueryFactory("excelFileName");
    var firstRow = (from c in excel.Worksheet()
                    select c).First();
    

    Checkout the rest of the documentation for LinqToExcel.