protected void Button1_Click( object sender, System.EventArgs e )
{
Response.Clear(); //清楚缓冲区流中内容
Response.Buffer = true; //设为缓冲输出
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
//导出Excel格式,其中filename为Excel文件的名字,可根据要求定义
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF_8; //设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush(); //刷新设计器加载程序的挂起更改
Response.End();
}
如果是将内容导出到word
则将上面的两行改为:
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.doc");
Response.ContentType = "application/ms-word";
就ok 了。
miss wang