C# 使用嵌套列表导出到excel
我想将我的模型导出到excel 模型C# 使用嵌套列表导出到excel,c#,asp.net-mvc-3,webforms,export-to-excel,C#,Asp.net Mvc 3,Webforms,Export To Excel,我想将我的模型导出到excel 模型 虽然这似乎是一个很老的问题,但我遇到了一个针对相同问题的实现,如下所示: 步骤1: 从数据/模型中创建JSON,如下所示: 使用Newtonsoft.Json; var jsonString=JsonConvert.SerializeObjectObjectObj 详情请浏览: 步骤2: 将数据从JSON导入excel。在我的例子中,它是Aspose,下面的链接中概述了这样做的方法。 即使您可能没有使用aspose,但excel导入过程仍然与之类似。您是否
虽然这似乎是一个很老的问题,但我遇到了一个针对相同问题的实现,如下所示: 步骤1: 从数据/模型中创建JSON,如下所示: 使用Newtonsoft.Json; var jsonString=JsonConvert.SerializeObjectObjectObj 详情请浏览: 步骤2: 将数据从JSON导入excel。在我的例子中,它是Aspose,下面的链接中概述了这样做的方法。
即使您可能没有使用aspose,但excel导入过程仍然与之类似。您是否尝试过ListoObject=x.List。其中…@NitinVarpe我一定会尝试tomorrow@NitinVarpe这个答案不幸被@e2e4否决了,原因是答案迟了。但这仍然是有帮助的,如果有人遇到类似的问题也会有所帮助。
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; }
}
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();
}