/// <summary>
/// GridView 导出 Excel或Word
/// </summary>
/// <param name="gv"></param>
/// <param name="fileType">文件类型</param>
/// <param name="fileName">文件名</param>
private void Exprot(GridView gv,String fileType, String fileName)
{
Response.Clear();
Response.BufferOutput = true;
//设定输出字符集
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
//设置输出流HttpMiME类型(导出文件格式)
Response.ContentType = fileType;
//关闭ViewState
Page.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter textWriter = new HtmlTextWriter(sw);
gv.RenderControl(textWriter);
//把HTML写回游览器
Response.Write(stringWriter.ToString());
Response.End();
Response.Flush();
}
//重写此方法
public override void VerifyRenderingInServerForm(Control control)
{
/*
* asp.net2.0设置了内部变量控制RenderControl不允许在Render方法之外被轻易调用。
* 如果不override VerifyRenderingInServerForm就会报错。
* 我们override void VerifyRenderingInServerForm(Control control)这个方法,
* 里面不写任何代码即可*/
//base.VerifyRenderingInServerForm(control);
}