如何在不使用任何第三方控件的情况下直接将MVC webgrid结果导出为pdf和excel?(iTEXtSharp我不工作)

时间:2013-09-28 18:37:55

标签: asp.net-mvc-4 webgrid

我已尝试过itextsharp样本并将序列化为xml。 我正在使用Entity frame work 6.0 和asp.net mvc4

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

我正在使用以下导出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。适合我。

<强> EDITED 我也在使用过滤器。您可以将其值从View发送到controller action并修改datasource。例如:

public ActionResult ExportData(DateTime fromDate)
            {
                var datasource = db.Products.Where(g=>g.Date <= fromDate).ToList();