简体   繁体   中英

How to convert from complex C# model to Excel spreadsheet with Interop

I am currently having an issue with my code, I am trying to convert from a C# model into excel worksheet using interop library. However I have tried everything possible but I have no results, could somebody helps me out with some ideas or workaround about how I could possibly implement such logic to reach my goal?

Model class C#

public class SampleModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public UserDetails UserDetails { get; set; }
}

public class UserDetails
{
    public int Id { get; set; }
    public string Email { get; set; }
    public int Location { get; set; }
}

And here is how this should look like:

最终结果的示例图像

I would appreciate any suggestion or help you can provide.

Assuming the workbook and worksheet and the needed columns are already existing and you have loaded the data into a List object...

Try something like this:

Excel.Application excelApp = new Excel.Application();
Excel.Workbook yourWorkbook = new excelApp.Workbooks["<full path to workbook>"];
Excel.Worksheet yourWorksheet = new yourWorkbook .Worksheets["<name of worksheet>"];

// This will just create the list of SampleModel objects.
// You will have to populate it with your data
List<SampleModel> data = new List<SampleModel>

// Start row counter at 2 because I am assuming the first row of your
// worksheet contains the headers (Id, Name, LastName, etc.)
int rowCounter = 2;

foreach (SampleModel row in data)
{
   rowCounter += 1;

   // I also assume Id is column 1, Name is column 2, etc.
   yourWorksheet.Cells[rowCounter, 1].Value2 = row.Id;
   yourWorksheet.Cells[rowCounter, 2].Value2 = row.Name;
   yourWorksheet.Cells[rowCounter, 3].Value2 = row.LastName;
   yourWorksheet.Cells[rowCounter, 4].Value2 = getUserDetailsString(row.UserDetails);
}

Then just make a helper function like this:

private string getUserDetailsString(UserDetails details)
{
   return $"Id = {details.Id.ToString()}\nEmail = {details.Email}\nLocation = {details.Location}";
}

Recommendation

I don't know the requirements of your project, but I would highly recommend getting rid of the extra UserDetails class and just having a single class with all your info like this:

public class SampleModel
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string LastName { get; set; }
   public string Email { get; set; }
   public string Location { get; set; }
}

This way there is not an extra Id (you should only need one id for all user details), and you wouldn't need the extra getUserDetailsString() helper function because you could just have your foreach loop like this:

foreach (SampleModel row in data)
{
   rowCounter += 1;

   yourWorksheet.Cells[rowCounter, 1].Value2 = row.Id;
   yourWorksheet.Cells[rowCounter, 2].Value2 = row.Name;
   yourWorksheet.Cells[rowCounter, 3].Value2 = row.LastName;
   yourWorksheet.Cells[rowCounter, 4].Value2 = row.Email;
   yourWorksheet.Cells[rowCounter, 5].Value2 = row.Location;
}

Hope this helps!

Let me know if you have any questions, and I can edit my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM