views:

635

answers:

4

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 to do that?

thanks

A: 

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).

Matthew Hood
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
A: 

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
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
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
A: 

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

used2could
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
A: 

hey i am not using MVC pattern. I am only using ASP.net 2.0 C#. I am using jqGrid 3.6 version.

I know that there is one property excelexport which need to be set true and we have to add one custom button and the on click of that button i have to call jqgrid.excelExport method. but i have to send on complex object to the page how do i send it?

Rupesh