 |
|
พอดีเจอเว็บไซต์ อ้างอิง : http://lakshmik.blogspot.com/2006/04/aspnet-export-to-excelword-from-nested.html
บอกขั้นตอนในการทำ รบกวนผู้รู้ค่ะ จากโค้ด ขอโค้ดตัวอย่างในการทำค่ะ ไม่รู้จะเริ่มยังไง ทำไม่เป็นค่ะ รบกวนด้วยนะคะ
ทำเป็น Web Application ภาษา VB ค่ะ
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 = "<style type=\"text/css\" id=\"style1\">.textbold {font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: #000000;font-weight: bold;text-decoration: none;}.row1 {background-color: #FFFFFF; font-family: Arial, Helvetica, sans-serif;font-size: 11px;color: #000000;height: 18px;padding-left: 5px;}.;
store.value = css + element.innerHTML;
}
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:
Code (C#)
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:
Code (C#)
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.
Tag : .NET
|
|
 |
 |
 |
 |
Date :
2014-02-19 21:09:12 |
By :
meaey |
View :
916 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |