gvShowExport.AllowPaging = false; //清除分页
gvShowExport.AllowSorting = false; //清除排序
string sqlString = " select * from dept_export where flag='" + flag + "' and addDate>='" + SearchTimeStart1.Text + "' and addDate<='" + SearchTimeEnd1.Text + "'";
gvShowExport.DataSource = DbHelperSQL.Query(sqlString);
gvShowExport.DataKeyNames = new String[] { "id" };
gvShowExport.Columns[8].Visible = false;
gvShowExport.DataBind(); //绑定gridview1数据源
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileNew.xls"); //.xls的文件名可修改
Response.ContentEncoding = System.Text.Encoding.UTF8;
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);
gvShowExport.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
gvShowExport.DataSource = DbHelperSQL.Query(sqlString);
gvShowExport.DataKeyNames = new String[] { "id" };
gvShowExport.AllowSorting = true; //恢复分页
gvShowExport.AllowPaging = true; //恢复排序
gvShowExport.DataBind(); //再次绑定
你这种用 GridView 导出Excel 不正规,生成的文件也不是真正 Excel 格式。
当前页面自动关闭应该不是导出Excel的作用吧,你加断点看看可能你那里谢了关闭页面的代码!!
页面关闭应该跟导出excel没什么关系的,看是不是有JS影响