我要將 GridView 的 HTML內容的資料匯出成 Excel 檔,這部份的功能可以透過Control.RenderControl的方式
Control.RenderControl : 輸出伺服器控制項內容,並在啟用追蹤功能的情況下,儲存控制項的追蹤資訊。
http://msdn.microsoft.com/zh-tw/library/system.web.ui.control.rendercontrol(VS.80).aspx
Control.RenderControl : 輸出伺服器控制項內容,並在啟用追蹤功能的情況下,儲存控制項的追蹤資訊。
http://msdn.microsoft.com/zh-tw/library/system.web.ui.control.rendercontrol(VS.80).aspx
將 GridView 內的資料匯出成 Excel 檔的程式碼
protected void Button2_Click(object sender,
EventArgs e)
{
Response.ClearContent();
string excelFileName = "測試Excel檔案.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(excelFileName));
Response.ContentType = "application/excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
{
Response.ClearContent();
string excelFileName = "測試Excel檔案.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(excelFileName));
Response.ContentType = "application/excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
在這邊會遇到兩個問題
問題1. 產生型別 'GridView' 的控制項 'GridView1' 必須置於有 runat=server 的表單標記之中的問題
這個問題我在 topcat 大大的Bog得到解決方式,加上此段程式碼即可
public override void VerifyRenderingInServerForm(Control control)
{
{
// '處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
}
}
關於此錯誤以及遇到網頁中有分頁時的處理方式,可以參考 topcat 大大的Blog
問題2. 當 GridView 中有中文字時,使用上述的程式碼所匯出的Excel會有中文亂碼
解決此問題的方式參考 MSDN 論壇上 LOLOTA 大大的方法,加上這段程式碼解決轉碼問題即可
Response.Write("<meta http-equiv=Content-Type
content=text/html;charset=utf-8>");
完整的程式碼如下 :
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Button2_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
string excelFileName = "測試Excel檔案.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(excelFileName));
Response.ContentType = "application/excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
// '處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Button2_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
string excelFileName = "測試Excel檔案.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(excelFileName));
Response.ContentType = "application/excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
// '處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
}
}
問題3. 當檔名中有中文字時,使用上述的程式碼所匯出的Excel會有中文亂碼
解決方法就是利用HttpUtility.UrlEncode來進行轉碼動作。
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", System.Web.HttpUtility.UrlEncode(FileName & ".xls", System.Text.Encoding.UTF8)))
問題4. RegisterForEventValidation 只能在 Render(); 期間呼叫。
這項網路上也有許多文章提出一個解法,大部分的人提出要在的標籤中,修改AutoEventWireup="true"並增加EnableEventValidation = "false";另一個人提出只需要增加EnableEventValidation = "false",並不需要將AutoEventWireup改為true。測試之後只需要增加EnableEventValidation = "false"屬性即可,但也有可能AutoEventWireup會解決另一個問題,還是先記錄下來吧!
問題5. 將GridView匯出excel,如果GridView有分頁的話則會發生錯誤。
// 先解除分頁
GridView1.AllowPaging = False;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
// 再設為分頁
GridView1.AllowPaging = True;
GridView1.DataBind();