Solved

Export to excel From MVC button click

Posted on 2016-07-19
2
148 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
ID: 41720955
try the below link, it provides a working sample
jqGrid ASP.NET - Export to Excel
0
 

Author Comment

by:Scott Baldridge
ID: 41721112
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Code works but it's slow 28 70
C# Comparing String Arrays. 5 39
Search an image for an image 3 27
Add different cell to otherwise similiar row 4 37
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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