Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Export to excel From MVC button click

Hello, I need to export some data to an excel document in an MVC application. I'm new to MVC and don't know how to react to the click event of the following button in the view.

<input type="button" value="@Resources.PortalAccountsMapping_ExportTitle" class="btnExport" />

How do I create an ActionResult that reacts when the button is clicked then exports data contained on the view inside a jqgrid table?
0
Scott Baldridge
Asked:
Scott Baldridge
1 Solution
 
Jitendra PatilSr.Software EngineerCommented:
try the below link, it provides a working sample
jqGrid ASP.NET - Export to Excel
0
 
Scott BaldridgeAuthor Commented:
Thank you Jitendra for your response but unfortunately I'm in an MCV application.

I have some code that does export the data I want, but there are some issues and I have a few questions.


1. When opening the excel file that is created I get this error:

The file format and extension don't match. The file could be corrupted or unsafe. unless you trust its source, don't open it. So you want to open it anyway?

How do I fix that? I need to export in .xlsx but if I change the file name with that extension in the export code the data does not populate the excel file.

2.  Instead of the @Html.ActionLink("Export to Excel", "ExportToExcel") I would prefer a button <input type="button".

How do I do that and get the Controller to execute public void ExportToExcel?

3. I'm sure it is not best practice to have the excel export code in the Controller.

How can I move all that to the Model and still accomplish the export.





[b]//link in view[/b]
@Html.ActionLink("Export to Excel", "ExportToExcel")


[b]//controller [/b]
 public void ExportToExcel(PortalAccountMappingModel model)

        {
            var grid = new System.Web.UI.WebControls.GridView();
            grid.DataSource = model.GetExport();
            grid.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
            Response.ContentType = "application/ms-excel";

            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }

[b]//Model[/b]
 public IEnumerable GetExport()
        {
            PortalAccountMappingReportProvider provider = new PortalAccountMappingReportProvider();
            var crt = new PortalAccountMappingSearchCriteria()
            {
                AccountCode = AccountCode ?? string.Empty,
                AccountName = AccountName ?? string.Empty,
                CustomerName = CustomerName ?? string.Empty,
                FirstName = FirstName ?? string.Empty,
                LastName = LastName ?? string.Empty,
                OnlyAccountWithoutMapping = OnlyAccountWithoutMapping
            };
            
            return provider.GetRows(crt);
        }

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now