-1

我已经尝试过 itextsharp 示例和序列化为 xml 。我正在使用实体框架 6.0 和 asp.net mvc4

4

2 回答 2

1

我正在使用以下导出到 excel:

在您的控制器中:

public ActionResult ExportData()
        {
            var datasource = db.Products.ToList(); 

            GridView gv = new GridView();
            gv.DataSource = datasource;         
            gv.DataBind();            
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Report.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();

           return Json("Success");
           }

在视图中:

@using (Html.BeginForm("ExportData", "Home", FormMethod.Post))
            {           

  <button type="Submit">Export to Excel</button>
            }

//OR:

@Html.ActionLink("Export to Excel", "ExportData", "Home")

至于导出为 PDF,我建议使用Rotativa。对我来说没问题。

编辑 我也在使用过滤器。您可以将其值发送Viewcontroller action并修改datasource。例如:

public ActionResult ExportData(DateTime fromDate)
            {
                var datasource = db.Products.Where(g=>g.Date <= fromDate).ToList();
于 2013-09-28T20:08:44.040 回答
1

尝试这个:

var data = GetFeedbackDetailsExport();

            var gridWeb = new WebGrid(source: data, canPage: false, canSort: false);
            string exportData = gridWeb.GetHtml(
                            columns: gridWeb.Columns(
                                                  gridWeb.Column("EnterpriseId", "EnterpriseId"),
                                                  gridWeb.Column("GroupName", "Group Name"),
                                                  gridWeb.Column("Geography", "Geography"),
                                                  gridWeb.Column("Country", "Country"),
                                                  gridWeb.Column("DeliveryCentre", "Delivery Centre"),
                                                  gridWeb.Column("Vendor", "Vendor Name"),
                                                  gridWeb.Column("Category", "Category"),
                                                  gridWeb.Column("ModelName", "ModelName"),                                            
                                                  gridWeb.Column("Status", "Status"),
                                                  gridWeb.Column("Date_Submitted", "Submitted On", format: (item) => item.Date_Submitted != null ? item.Date_Submitted.ToString("dd/MMM/yyyy") : ""),
                                                  gridWeb.Column("UpdatedBy", "Feedback Given By", style: "col-lg-1"),
                                                  gridWeb.Column("UpdatedOn", "Feedback Given On", format: (item) => item.UpdatedOn != null ? item.UpdatedOn.ToString("dd/MMM/yyyy") : "")
                 )
                                ).ToHtmlString();
            return File(new System.Text.UTF8Encoding().GetBytes(exportData),
                    "application/vnd.ms-excel",
                    "FeedbackReport.xls");
于 2016-08-03T07:12:51.427 回答