using LinqToExcel;
using System.Collections.Generic;
public class ExcelAndCSVFileUtility
{
public static IEnumerable<T> ReadRecords<T>(string fileName,string sheetName)
{
var items = new ExcelQueryFactory(fileName)
.Worksheet<T>(sheetName);
foreach (var item in items) yield return item;
}
}
We are creating an instance of the ExcelQueryFactory class by passing the full path to the Excel spreadsheet or CSV file.Then we are invoking the Worksheet method that expects the sheetName. Though, it is however optional.This method enables Linq queries against an Excel worksheet.We need to pass the TSheetData type to the Worksheet method. The TSheetData class returns the excel's row data as the specific Class Type .The yield moves the program pointer to and fro between caller and the collection.