1

My question relates to EPPlus 4.5.2.1 and the .LoadFromCollection() method. I am using this in a .NET Core 2.1 MVC project to retrieve data from the DB into a model and push it into an Excel file for download.

Let's say we have an object:

namespace ThingsToDo
{
    public class ActivityDate
    {
        DateTime date { get; set; }
        String timeSpan { get; set; }

        public override string ToString()
        {
            return $"{date.ToShortDateString()} {timeSpan}";
        }
    }
}

Then we have another object (model) that uses this:

namespace ThingsToDo
{
    public class ActivityInfo
    {
        public string ActivityName { get; set; }
        public ActivityDate ActivityDate { get; set;}
    }
}

For brevity, assume a List contains objects (all data is non-nullable).

If I do something like:

public IActionResult getExcel()
{
        var pck = new ExcelPackage();

        //Run my model's query
        List<ActivityInformation> model = //See above, assume this list has data in it

        //Create a Worksheet
        var wsToReturn = pck.Workbook.Worksheets.Add("Sheet 1");


        wsToReturn.Cells["A1"].LoadFromCollection(model, true, OfficeOpenXml.Table.TableStyles.Medium9);

        return File(pck.GetAsByteArray(), FILE_TYPE_EXCEL, "Report.xlsx");
}

This will return: Workbook with Object Namespace qualified names.

I've read the references and looked through their samples (which are excellent if you're a person reading this and are just looking), however cannot seem to figure out if it is possible to have it parse out the proper way to display the ActivityDate via its to string.

Or am I relegated to creating an "ExcelOutput" object where the date has been split out already?

I believe this is related to: Question #34209678, so I bet I'm out of luck!

Thanks for any suggestions and help!

Edited for adding "public" to classes (they are actually declared as public, I omitted them for no specific reason; laziness possibly.)

Avogadro
  • 353
  • 2
  • 11
  • 1
    Could this be due to the accessibility of the class? - i.e. have you tried making `ActivityDate` a `public` class? – SpruceMoose Aug 14 '18 at 16:06
  • Hi! Thanks for the thought! However, they are public, I just omitted that here. I will edit for clarity. – Avogadro Aug 14 '18 at 16:40

0 Answers0