Solved

Export to excel From MVC button click

Posted on 2016-07-19
2
130 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

861 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

24 Experts available now in Live!

Get 1:1 Help Now