gridview导出到excel出现问题
gridview分页为每页14条记录,导出时是都导出。当查询的记录很多时导出正常当查询的记录只有一页或两页时就不会出现选择导出的excel文件存放位置的对话框,所以就应该...
gridview分页为每页14条记录,导出时是都导出。
当查询的记录很多时导出正常
当查询的记录只有一页或两页时就不会出现选择导出的excel文件存放位置的对话框,所以就应该算是导出不成功吧,请问高手这是什么情况?怎么解决?
下面是导出excel的代码
gvData.AllowPaging = false;
gvData.DataBind();
//导出文件的文件名
string fileName = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
//导出的具体实现逻辑
//防止出现乱码,加上这行可以防止在只有一行数据时出现乱码
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
//Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
gvData.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
gvData.RenderControl(htmlWriter);
//输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
gvData.AllowPaging = true; 展开
当查询的记录很多时导出正常
当查询的记录只有一页或两页时就不会出现选择导出的excel文件存放位置的对话框,所以就应该算是导出不成功吧,请问高手这是什么情况?怎么解决?
下面是导出excel的代码
gvData.AllowPaging = false;
gvData.DataBind();
//导出文件的文件名
string fileName = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
//导出的具体实现逻辑
//防止出现乱码,加上这行可以防止在只有一行数据时出现乱码
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
//Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
gvData.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
gvData.RenderControl(htmlWriter);
//输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
gvData.AllowPaging = true; 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询