1

I'm having an issue handling one column of the Excel file I'm trying to import to my database. The property refers to an Enum model. I tried putting integers values (referring to the Enum index) or the string value themselves but obviously it doesn't work.

How would I go about handling this using (I assume) the AddTransformation method?

Orphu.of.io
  • 125
  • 1
  • 16

4 Answers4

2

I was able to accomplish this by mapping the value from the cell to the enum value in the transformation function:

excel.AddTransformation<DataImportJob>(x => x.Status, cellValue =>
{
    switch (cellValue)
    {
        case "Failed":
            return JobStatusType.Failed;
        case "InProgress":
            return JobStatusType.InProgress;
        case "Scheduled":
            return JobStatusType.Scheduled;
        case "Success":
            return JobStatusType.Success;
        default:
            return JobStatusType.Undefined;
    }
});
0

Yea, try the AddTransformation method and see if that works.

Paul
  • 18,349
  • 7
  • 49
  • 56
  • Hi Paul and thanks for the great tool. Would you have a suggestion how to use AddTransformation in this case? I've tried a few things but failed at it. I always get an exception that it can't cast a double (from the excel cell) to the Enum type it should refer to. Actually not sure why it thinks the value is a double since they are clearly integers in the excel... – Orphu.of.io May 02 '14 at 19:00
  • Figured it out and posted my solution. It might be possible with AddTransformation too but opted otherwise. – Orphu.of.io May 03 '14 at 10:12
0

The solution was to create a new object and map the property manually. The Enum was handled this way:

MyEnumProperty = (MyEnumProperty)Convert.ToInt32(c["ColumnNameInExcel"])
Orphu.of.io
  • 125
  • 1
  • 16
0

I tried the AddTransformation method with Invalid cast from Double to enum issue.

excelFile.AddTransformation<Table>(x => x.Company, cellValue => (MyEnumProperty)Convert.ToInt32(cellValue));
Sha-Pai Li
  • 157
  • 3
  • 15