gridview 控件导出到excel 数据丢失,问了好多人都没有搞定。
页面显示数据都是正常的,但是导出到excel数据都是0呀。是基于oracle的查询。字段类型是number(5,1),这个是页面显示,下面这个是导出的结果,怎么全部是0了...
页面显示数据都是正常的,但是导出到excel 数据都是0 呀 。是基于oracle的查询。字段类型是number(5,1),
这个是页面显示,下面这个是导出的结果,怎么全部是0了呢。
导出部分代码:
GridView1.DataSourceID = "ObjectDataSource1"; // 确定数据源 GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=rain.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); GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();
真是很郁闷 ,问了几个人都没有搞定呀 展开
这个是页面显示,下面这个是导出的结果,怎么全部是0了呢。
导出部分代码:
GridView1.DataSourceID = "ObjectDataSource1"; // 确定数据源 GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=rain.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); GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End();
真是很郁闷 ,问了几个人都没有搞定呀 展开
1个回答
展开全部
我也是使用gridview导出的但是没有出现问题 我怀疑你是没有把数据显示成文本形式 你看下我代码
public void Export(System.Web.UI.Control control)
{
Response.Clear();//清除缓冲区流中所有内容的输出
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
//字符集设置
GridView1.Attributes.Add("style", "Vnd.ms-excel.numberformat: @");//以文本形式显示数据 :解决形如0001变成1的问题;
Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
control.Page.EnableViewState = false;
this.GridView1.AllowPaging = false; //这里是有分页的重新绑定可以把所有都导出
this.GridView1.Columns[8].Visible = false;//隐藏修改
this.GridView1.Columns[9].Visible = false;//隐藏删除
下面是你重新绑定的代码
GridView1.DataSourceID = "ObjectDataSource1"; // 确定数据源
GridView1.DataBind();
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
control.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.Write("<script>alert('导出成功!')</script>");
//最后在分页绑定OK!!!
this.GridView1.AllowPaging = true;
GridView1.DataSourceID = "ObjectDataSource1"; // 确定数据源
GridView1.DataBind();
Response.End();
}
public void Export(System.Web.UI.Control control)
{
Response.Clear();//清除缓冲区流中所有内容的输出
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
//字符集设置
GridView1.Attributes.Add("style", "Vnd.ms-excel.numberformat: @");//以文本形式显示数据 :解决形如0001变成1的问题;
Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
control.Page.EnableViewState = false;
this.GridView1.AllowPaging = false; //这里是有分页的重新绑定可以把所有都导出
this.GridView1.Columns[8].Visible = false;//隐藏修改
this.GridView1.Columns[9].Visible = false;//隐藏删除
下面是你重新绑定的代码
GridView1.DataSourceID = "ObjectDataSource1"; // 确定数据源
GridView1.DataBind();
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
control.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.Write("<script>alert('导出成功!')</script>");
//最后在分页绑定OK!!!
this.GridView1.AllowPaging = true;
GridView1.DataSourceID = "ObjectDataSource1"; // 确定数据源
GridView1.DataBind();
Response.End();
}
追问
兄弟呀我用你的这个方法更是郁闷了,导出的excel 就是空白了连字段都没得了。能不能加个 ,方便交流呀 。三八三零壹五二三陆
追答
不应该啊 !! 你的gridview有数据吗?? 如果gridview中有数据的话 那就应该能导出了。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询