private void ExportGridView(GridView gridview, string fileType, string fileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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);
gridview.RenderControl(hw);
//new ClientScriptManager().RegisterForEventValidation(new PostBackOptions());
Response.Write(tw.ToString());
Response.End();
//fileType is "application/ms-word" and "application/ms-excel"
// ExportGridView(this.GridView1, "application/ms-excel", "aa.xls");
// ExportGridView(this.GridView1,"application/ms-word", "Word.doc"));
}
//必须列出该方法
public override void VerifyRenderingInServerForm(Control control)
{
}
如何将GridView控件数据导出到word和Excel
最新推荐文章于 2017-11-13 20:01:00 发布