When you say DataTable, do you mean System.Data.DataTable so you can use it in a DataGrid control? While ClosedXML can take a DataTable or DataSet or even a List of objects and automatically create an Excel worksheet out of it, there is nothing I know of that would do the reverse. It would be a nice feature if it could. Even the methods CreateTable() and AsTable() do not create DataTable objects. They create IXLTable objects.
As far as I know, in order to do what you want, you could iterate through the range and build your DataTable one row at a time.
You could probably create a LINQ expression on the your IXLRange object that would return a IENumerable list of structures.
That would be a pretty succinct and classy piece of coding. I'd love to see a sample of that code put into the documentation area.
I am routinely creating Lists of objects representing data rows. I would love to see an enhancement where I could pass an object into ClosedXML where the properties of the object had the same names as columns in the Excel sheet and have ClosedXML automatically fill in the data fields and return me a list of the objects. Even if it would do it one row at a time, that would be heaven.
If that exists you could define your DataRow and then fill your DataTable.Rows collection with the rows from the Excel sheet.
As far as I know, in order to do what you want, you could iterate through the range and build your DataTable one row at a time.
You could probably create a LINQ expression on the your IXLRange object that would return a IENumerable list of structures.
That would be a pretty succinct and classy piece of coding. I'd love to see a sample of that code put into the documentation area.
I am routinely creating Lists of objects representing data rows. I would love to see an enhancement where I could pass an object into ClosedXML where the properties of the object had the same names as columns in the Excel sheet and have ClosedXML automatically fill in the data fields and return me a list of the objects. Even if it would do it one row at a time, that would be heaven.
If that exists you could define your DataRow and then fill your DataTable.Rows collection with the rows from the Excel sheet.
// Get a list of objects from the Excel sheet
List<MyObject> myList = new List<MyObject>();
foreach ( IXLRow row in workSheet.RangeUsed(false).Rows() )
{
MyObject myObject = new MyObject();
myList.Add(row.FillObject(myObject));
}
Wouldn't that be grand! Too bad it doesn't exist yet.