0

I want to convert an ExcelRange object into a model object

Currently, I have the code to get the range in a row

var row = sheet.Dimension.Start.Row + 1;
var lastCol = sheet.Dimension.End.Column;
var endRow = sheet.Dimension.End.Row;

for (; row <= endRow; row++)
{
    var range = sheet.Cells[row, 1, row, lastCol];
    // code to add here to convert
}

I don't know how to convert this into c# object. The range.Value has object but I don't know how to convert this to a c# model.

EDIT:

My model look like this:

var contact = new ContactHubSpot
        {
            Id = contactId,
            FirstName = candidate.FirstName,
            LastName = candidate.LastName,
            Gender = candidate.GenderId.HasValue ? candidate.Gender.Name : null,
            BirthDate = candidate.BirthDate.HasValue ? candidate.BirthDate.Value.ToMidnightTimestamp() : (long?)null,
            Phone = candidate.MobileInternational,
            Email = string.IsNullOrEmpty(candidate.EmailAddress) ? null : candidate.EmailAddress,
            Bio = candidate.Bio,
            IsEmployed = candidate.IsCurrentlyEmployed,
            IsOpenToOffers = candidate.IsOpenToOffers,
            NoticePeriod = candidate.IsCurrentlyEmployed ? $"{candidate.NoticePeriod} {candidate.NoticePeriodTimeframe.Name}" : string.Empty,
            IsLookingForEmployment = candidate.IsLookingForEmployment,
            IsLookingForFreelance = candidate.IsLookingForFreelance,
            DrivingLicense = candidate.DrivingLicenseId.HasValue ? candidate.DrivingLicense.Name : string.Empty,
            ExperienceLevel = candidate.ExperienceLevelId.HasValue ? candidate.ExperienceLevel.Name : string.Empty,
            IsCandidate = true,
            IsEmployer = false
        };
Rashid
  • 1,700
  • 1
  • 23
  • 56
  • Can you share what your `model object` looks like? – WSC Oct 08 '20 at 11:45
  • @WSC done adding the model – Rashid Oct 08 '20 at 11:49
  • Seems like you're basically there. All you need to do now is create a `new ContactHubSpot` object and assign the various properties from your `range` (which by the looks of it is just a row of data). Presumably you know which order your columns are in (or have some prior code which formats the data as you're expecting). You can access a specific cell in `range` by doing `range.Item(col, row)`. – WSC Oct 08 '20 at 13:07
  • @WSC yup, I know I can do it that way. But what I want is to simplify everything like this one https://stackoverflow.com/a/37746915/5870896 – Rashid Oct 08 '20 at 14:15
  • So do what is shown in that question? I'm not really sure what your question is; you know what you want to do, you have one method of doing it, and you have an example of an alternative. You need to at least try a solution and then ask a question if you have problems. – WSC Oct 08 '20 at 14:35
  • @WSC well before I ask this problem, I did not found out that solution yet. – Rashid Oct 08 '20 at 15:07

0 Answers0