首页 新闻 搜索 专区 学院

C# 实现gridview导出Excel表格,导出后,当前页面自动关闭了,怎么解决?

0
悬赏园豆:10 [待解决问题]

       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(); //再次绑定

c#
mm化身为海的主页 mm化身为海 | 初学一级 | 园豆:192
提问于:2013-04-22 15:26
< >
分享
所有回答(3)
0

你这种用 GridView 导出Excel 不正规,生成的文件也不是真正 Excel 格式。

geass.. | 园豆:1705 (小虾三级) | 2013-04-22 15:51
0

当前页面自动关闭应该不是导出Excel的作用吧,你加断点看看可能你那里谢了关闭页面的代码!!

Kencery | 园豆:357 (菜鸟二级) | 2013-04-22 17:25
0

页面关闭应该跟导出excel没什么关系的,看是不是有JS影响

骷髅人 | 园豆:464 (菜鸟二级) | 2013-04-22 17:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册