Avatar of Scott Baldridge
Scott Baldridge
 asked on

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?
.NET ProgrammingC#

Avatar of undefined
Last Comment
Scott Baldridge

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jitendra patil

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Scott Baldridge

ASKER
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

Your help has saved me hundreds of hours of internet surfing.
fblack61