0

How to export ASP.NET MVC view data to an excel file ? Actually my view page contain many viewdata types. I am using for each loop with these datatype to displaying data on the view page. My requirement is that I want to export this displayed data into Excel file. How do I achieve this?

Thanks

Darren
  • 68,902
  • 24
  • 138
  • 144
sandeep
  • 77
  • 1
  • 4
  • 9
  • http://stackoverflow.com/questions/301986/export-to-excel-in-asp-net-mvc http://stackoverflow.com/questions/1948799/generate-an-excel-xml-document-in-asp-net-mvc-web-site – Mauricio Scheffer Apr 21 '10 at 12:23

3 Answers3

1

Look at http://blogs.msdn.com/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx.

After some experiments I use Open XML SDK 2.0. You can find currently a lot of information how to do this on http://msdn.microsoft.com/en-us/office/aa905545.aspx and http://openxmldeveloper.org/. One more link http://extrememl.codeplex.com/ can be also useful.

Usage of Open XML SDK 2.0 at the first time take a little more time as other ways, but exported files are real Excel files and should not be converted by users. And if you will be have more requirements for the format of produced excel files, you will be able to implement there.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • thanks for replay but my requirement is that: i need to display (mvc) employee(view page) that displays a empdetails,on that page i want a one link i.e export to excel.when i click that link i need generate a excel file of empdetails i.e displayed on the employee(view page) – sandeep Apr 21 '10 at 12:53
  • I don't think, that displaying of master/detail views is a problem. One can find a lot of corresponding examples. There are a lot of different ways to do so depends on your requirements. So I answered only on the "export to excel" part of your question. You don't specify in which form do you want to display the information. If you want use some table control, I could recommend jqGrid http://www.trirand.com/blog/?page_id=6. Corresponding master/detail example you can find on http://trirand.com/blog/jqgrid/jqgrid.html, choose "Advanced" on the left side and then "Master Detail". – Oleg Apr 21 '10 at 13:20
0

You could loop through the Keys/Values of your ViewData and generate the excel file (various options mentioned Here) or simply to CSV

But I would recommend rather doing this in the controller and returning a FileResult and not from the ViewData (which I presume you using in your View).

Community
  • 1
  • 1
Matthew Hood
  • 958
  • 3
  • 13
  • 22
  • thanks for replay but my requirement is that: i need to display (mvc) employee(view page) that displays a empdetails,on that page i want a one link i.e export to excel.when i click that link i need generate a excel file of empdetails i.e displayed on the employee(view page) – sandeep Apr 21 '10 at 12:52
0

Depending on the complexity of the page's HTML you might be able to get away with just changing the header type to application/excel. Excel has built in html parsing functionality. I've successfully done this many times a few years back with a DataGrid on a page.

Response.ContentType = "application/excel"
Response.AppendHeader "content-disposition", "attachment: filename=TestExcelFile.xls"

Some details on HTML in excel

Brian
  • 4,974
  • 2
  • 28
  • 30
  • thanks for replay but my requirement is that: i need to display (mvc) employee(view page) that displays a empdetails,on that page i want a one link i.e export to excel.when i click that link i need generate a excel file of empdetails i.e displayed on the employee(view page) – sandeep Apr 21 '10 at 12:52