標籤:
//匯出excel按鈕事件
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "農田環境資料.xls");
}
//匯出word按鈕事件
protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "農田環境資料.doc");
//Export("application/ms-word", "農田環境資料.doc");//都可以
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw); Response.Write(tw.ToString());
Response.End();
}
// 此方法必重寫,否則會出錯
public override void VerifyRenderingInServerForm(Control control)
{
}
C# GridView匯出excel和word檔案