GridView to excel
from http://youlearnnote.blogspot.com/2017/03/grid-viewexcel.html
protected void btn_excel_Click(object sender, EventArgs e)
{
//呼叫的片段、方式
// export_excel(要匯出的 Gridview 名稱, 匯出的檔名,模式); // 1=會加入日期時間
export_excel("gridview2", "output", 1);
}
private void export_excel(string gvname, string filename, int t_mode)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=test.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=big5>");
HttpContext.Current.Response.Write("<head><meta http-equiv=Content-Type content=text/html;charset=big5></head>");
Response.Charset = "big5";
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView2.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
留言
張貼留言