The easiest way to do this is to cast both the DataTableCollection
and DataColumnCollection
so you can use normal Linq queries over them.
For example:
var stream = File.Open(@"C:\temp\Book1.xlsx", FileMode.Open, FileAccess.Read);
var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
var result = excelReader.AsDataSet();
var tables = result.Tables
.Cast<DataTable>()
.Select(t => new {
TableName = t.TableName,
Columns = t.Columns
.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToList()
});
The reason the cast is necessary is because both DataTableCollection
and DataColumnCollection
only implement IEnumerable
and not IEnumerable<T>
as they date back to the days before generics.
For this code to work your spreadsheet has to actually contain defined tables not just have data in it.