Convert row in Excel to custom entity
In order to read data from an Excel sheet I'm using a third-party dll that has one method GetCell
and its return type is Object
. One row in the Excel sheet contains multiple columns so after reading one row I'm stuck with a lot of Objects. These objects should be the properties of a cu开发者_开发技巧stom entity. For instance, if the Excel sheets has the two columns Name
and DateOfBirth
, then the values should map to the following Person entity
class Person{
public string Name {get; set;}
public DateTime DateOfBirth {get; set;}
}
Is there a simple way of doing this conversion? Since I have to read many different Excel sheets, the conversion can not be hard-coded.
I'm assuming you're using C#. One possibility I could think of was keeping a List<List<string>> setsoflabels
in which you would hold the names of the columns associated with a particular entity.
So the first entry in setsoflabels
would be a list comprised of "Name"
and "DateofBirth"
.
Then, you could have a List<object> types
of which the first entry was Person
.
(The second entry in setsoflabels
could be a list comprised of "Make"
and "Model"
, and the second entry in types
could be Car
, assuming you have a class with that name)
If a particular sheet's headings matched up with a particular index of setsoflabels
then check that same index in types
to get the type.
It's essentially like having a dictionary, but I don't believe you can have a dictionary with a List
as a key. You could probably whip up a custom data structure based on what I have outlined above if you are going to need it repeatedly.
精彩评论