Wednesday 8 August 2012

Export to Excel/Word from Nested GridViews

Exporting a GridView to an Excel/Word is one common requirement in ASP.NET applications. In case of simple GridViews this is a pretty easy and the code for the same can be found in my earlier article.

In case of Nested GridViews, when trying to export to Word/Excel, the output comes would always be rendered inverted in Word/Excel.

In this scenario, let us see how we can export the entire Nested GridView.

The first step is to insert a <div > that embeds the entire Nested GridView.
For Ex :



<div id="divNestedGrid" runat="server">
<asp:GridView id=.. >
...
<asp:GridView .... />
---
</asp:GridView>
</asp:GridView>
</div>

Next step is add a hidden variable in the same aspx form.

<input type="hidden" id="hdnInnerHtml" value="" runat="server" />

Now add a javascript function that in the aspx that fills the hidden variable with the inner html of the div.




function getInnerHtml()
{
var element = document.getElementById("divpreview");
var store = document.getElementById("hdnInnerHtml");
//add the css styles you have used inside the nested GridView
var css = "
Now in the Code Behind, first add the javascript function to be triggered on the click on the Export button.


btnExport.Attributes.Add("OnClick", "getInnerHtml();");

Finally, write the following code in the Export Button click event, to Export to Word:


string html = hdnInnerHtml.Value;
Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
Response.Clear();
Response.AppendHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-word";
this.EnableViewState = false;
Response.Write("\r\n");
Response.Write(html);
Response.End();

In case of Exporting to Excel, you can change the code as follows:



string html = hdnInnerHtml.Value;
Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
Response.Clear();
Response.AppendHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
Response.Write("\r\n");
Response.Write(html);
Response.End();

In this article we discussed different ways of Exporting Data to Excel and Word when dealing with Nested Grid Views.

4 comments:

Vara said...


Hi please check out this it may help some one...
Excel :
http://reddyinfosoft.blogspot.in/2012/08/export-gridview-data-to-excel-using.html[^]

PDF :
http://reddyinfosoft.blogspot.com/2012/08/export-gridview-data-to-pdf-using.html[^]

and Word :

http://reddyinfosoft.blogspot.com/2012/08/export-gridview-data-to-word-using.html

Mahad Bin Mukhtar said...

Thanks Vara Reddy..

Pranav Singh said...

Hi this is really nice . have a look of this article also.
http://www.dotnetpools.com/2012/09/gridview-export-to-excel-in-aspnet-c.html

Anonymous said...

Export data to excel from ketticgridview

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More