1

I want to export my model to excel

Model

public class CaseModel
{
    public int CaseId { get; set; }
    public long TenantId { get; set; }
    public string CaseDescription { get; set; }        
    public List<CustomFields> CustomFields { get; set; }       
    public List<CustomFieldForUsers> CustomFieldForUser { get; set; }
}

I am getting predefined fields in excel but i am not getting list objects i.e CustomFields and CustomFieldForUser

Controller

public void ExportAsExcel()
{
    List<CaseModel> lst = new List<CaseModel>();
    if (Request.QueryString["caseids"] != null)
    {
        string[] collection = Request.QueryString["caseids"].Split(',');
        foreach (var item in collection)
        {
            var model = caseservice.getCase(Tenant.Id, int.Parse(item));
            model.CustomFieldForUser = caseservice.getCustomFieldsForUser(Tenant.Id, int.Parse(item));
            model.CustomFields = caseservice.getCustomFields(Tenant.Id);
            lst.Add(model);
        }
    }
    var result = lst.Select(x => new
    {
        x.RefId,
        x.CaseDescription,
        x.CreatedBy,
        CreatedDate = String.Format("{0:MMM d, yyyy}", x.CreatedDate),
        CaseStatus = Enum.GetName(typeof(CaseStatus), x.CaseStatus),
        CasePriority = Enum.GetName(typeof(CasePriority), x.CasePriority),
        AssignedTo = x.AssignedTo == null || x.AssignedTo == "0" ? "NA" : tenantservice.getTenantUserById(Tenant.Id, x.AssignedTo).RealName,
        Company = x.CompanyId == 0 ? "NA" : companydetailservice.getCompanyDetails(Tenant.Id, x.CompanyId).Name,

       **//How to get the List objects here to be downloaded in excel since they are dynamic based on user who has loggedin**

    });


    GridView gv = new GridView();
    gv.DataSource = result;
    gv.DataBind();
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=" + Tenant.SubDomain + "_Cases_" + DateTime.Now.ToShortDateString() + ".xls");
    Response.ContentType = "application/ms-excel";
    Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gv.RenderControl(htw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73

1 Answers1

-1

Although it seems a pretty old questions but I came across an implementation for the same issue as below:

Step 1:

Create JSON out of the data/model as below: using Newtonsoft.Json; var jsonString = JsonConvert.SerializeObject(obj);

More details at : How do I turn a C# object into a JSON string in .NET?

Step 2:

Import Data from JSON to excel. In my case it was Aspose and the way to do so is outlined in the below link. https://docs.aspose.com/cells/net/import-data-into-worksheet/#importing-data-from-json

Even if you may not be using aspose but the excel import process remains similar to it.

man4code
  • 29
  • 4
  • This answer was unfortunately down voted by @e2e4 with reason as "Late Answer". But it will still be helpful and would help if someone runs into similar issue. – man4code Jan 22 '21 at 02:15