protected void btnWord_Click(object sender, EventArgs e)
{ GridView1.AllowPaging = false;//清楚分页 GridView_BindByDate(); Response.Clear(); Response.BufferOutput = true; //设定输出的字符集 Response.Charset = "GB2312"; //假定导出的文件名为FileName.doc Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.doc"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); ////设置导出文件的格式 Response.ContentType = "application/ms-word"; //关闭ViewState this.EnableViewState = false; CultureInfo cultureInfo = new CultureInfo("ZH-CN", true); StringWriter stringWriter = new StringWriter(cultureInfo); HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter); GridView1.RenderControl(textWriter); ////把HTML写回浏览器 Response.Write(stringWriter.ToString()); Response.Flush(); Response.End(); //恢复分页 GridView1.AllowPaging = true; //为GridView重新绑定数据源 GridView_BindByDate(); } 在上文红色行后加上 Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); |
|