Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//attachment改成online,成为在线打开
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Today.ToShortDateString() + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
GridView1.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
Response.Buffer = true;
Response.Charset = "GB2312";
//attachment改成online,成为在线打开
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Today.ToShortDateString() + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
GridView1.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
要存为word,文本,只要将相应的后缀后为.doc或.txt就可以了..这段代码会在客户端弹出另存为
!注意 :这个GridView不能保含分页,排序,模板,选定内容.总之就是存表格式的.否则会发生异常---->