Solved

Export to excel From MVC button click

Posted on 2016-07-19
2
92 Views
Last Modified: 2016-07-21
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
Comment
Question by:Scott Baldridge
2 Comments
 
LVL 12

Accepted Solution

by:
Jitendra Patil earned 500 total points
Comment Utility
try the below link, it provides a working sample
jqGrid ASP.NET - Export to Excel
0
 

Author Comment

by:Scott Baldridge
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now