For a recent project of mine, I needed to be able to read Excel files and I came across an excellent open source library called EPPlus. Unfortunately, I couldn't find any generic methods to cast the rows or columns to objects so I decided to create these myself.
Here, we have 2 public methods for reading one specific mapping or get all mappings returned in a list. public static TItem GetRecord<TItem>(this ExcelWorksheet sheet, int rowOrColumn, ExcelMap<TItem> map = null) public static List<TItem> GetRecords<TItem>(this ExcelWorksheet sheet, ExcelMap<TItem> map = null)
If you would like to supply your own mapping, all you need to do is derive from ExcelMap<TItem> and fill the Mapping dictionary. Another way of indicating the relation between the Excel rows or columns and the properties of TItem is by the use of attributes.
[
add
]
[
|
|
...
]