How to export MVC webgrid results to pdf and excel directly without using any third party control?(iTEXtSharp i not working)

StackOverflow https://stackoverflow.com/questions/19070373

  •  29-06-2022
  •  | 
  •  

문제

I have tried itextsharp samples and the serializing to xml . Am using Entity frame work 6.0 and asp.net mvc4

도움이 되었습니까?

해결책

I'm using following for export to excel:

In your Controller:

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");
           }

In View:

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

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

//OR:

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

As to export to PDF, I'd recommend to use Rotativa. Works ok for me.

EDITED I'm using filters too. You may send it's value from View to controller action and modify datasource. For example:

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

다른 팁

Try this:

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");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top