GridView如何将分页数据全部导出为EXCEL?(附我的代码)

whut0209 2007-10-24 03:33:12
我已实现将GridView导出至Excel,但由于我的Gridview是分页的,所以导出Excel时只能导出当前页的数据,我已在后台设置了

GridView1.AllowPaging = false;

但这句话好像没起作用。现贴上代码,请各高手指教!


前台有一个GridView控件,和Button控件。
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="serialID" DataSourceID="SqlDataSource2">
</asp:GridView>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出至Excel" />

后台代码为:

protected void Button1_Click(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";

Response.ClearContent();

Response.AddHeader("content-disposition", "attachment; filename=ShipExcelFile.xls");

Response.ContentType = "application/ms-excel";

Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false; //就是这句话,怎么不起作用那?

GridView1.RenderControl(htw);

Response.Write(style);

Response.Write(sw.ToString());

Response.End();

}

public override void VerifyRenderingInServerForm(Control control)
{

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("class", "text");
}
}
...全文
2251 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
ritchie511 2011-02-24
  • 打赏
  • 举报
回复
可以了
glume123 2011-01-11
  • 打赏
  • 举报
回复
不能分页啊。
glume123 2011-01-11
  • 打赏
  • 举报
回复
9楼的方法可以,好了
yunyuyao 2010-11-21
  • 打赏
  • 举报
回复
9楼可以,收藏
Lexiaoyao20 2010-08-13
  • 打赏
  • 举报
回复
9楼的方法确实能行。顶
wangxiaofeiwuqiao 2010-07-10
  • 打赏
  • 举报
回复
9楼方法可行,我试过了,收藏
elvafans_hehe 2010-06-26
  • 打赏
  • 举报
回复
这个帖子需要收藏
netccb 2010-03-11
  • 打赏
  • 举报
回复
的确,我的也可以了

个人签名

美女图片站:http://www.zaomm.com
leo2254776 2008-08-06
  • 打赏
  • 举报
回复
lz的问题我刚好碰到,不是你的思路有误,而是在

GridView1.AllowPaging = false; //就是这句话,怎么不起作用那?

这个之后要加上:

gridview1.DataBind();

就行了
honghuali 2007-11-09
  • 打赏
  • 举报
回复
~~~!
simachangan 2007-10-25
  • 打赏
  • 举报
回复
protected void Button2_Click(object sender, EventArgs e)
//按button2将gridview将数据导出。
{
GridView1.AllowPaging = false; //清除分页
GridView1.AllowSorting = false; //清除排序
pbind(); //你绑定gridview1数据源的那个函数。
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=dpdgxkh.xls"); //.xls的文件名可修改
Response.ContentEncoding = System.Text.Encoding.UTF7;
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();
GridView1.AllowSorting = true; //恢复分页 GridView1.AllowPaging = true; //恢复排序
pbind(); //再次绑定 }
yeyu170 2007-10-25
  • 打赏
  • 举报
回复
Response的时候把GridView的数据源导出来就可以了。
随便你GridView是怎么处理的。
qilinshu 2007-10-25
  • 打赏
  • 举报
回复
//导出的方法
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
// turn off paging
gv_KeywordManage.AllowPaging = false;
BindGrid();

gv_KeywordManage.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

// turn the paging on again
gv_KeywordManage.AllowPaging = true;
BindGrid();

}

关键是AllowPaging的false和true. 找我的结构写就可以了.
whut0209 2007-10-25
  • 打赏
  • 举报
回复
谢谢德仔,我好好看看!
whut0209 2007-10-25
  • 打赏
  • 举报
回复
再次感谢各位!

我发现自己的GridView的数据绑定是在前台实现的,没有在后台用到类似BindData()的函数。是不是这个就是问题所在

阿?

如果要实现分页数据的全部导出,一定要在后台绑定数据吗?前台绑定数据比较直观,而且操作简便。真的不想在后台重新写

代码了…………

各位还有什么高招吗?
德仔 2007-10-24
  • 打赏
  • 举报
回复
可以写一个存储过程,直接调用就可以了



asp.net导出/导入规则的Excel(.xls)文件
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=17407


扩展GridView之自带Excel和Word导出
http://www.dezai.cn/Article_Show.asp?ArticleID=17149

SQL语句导入/导出EXCEL
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=16581

将datagrid数据导入到excel的方法
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=15031

将datagrid数据导入到excel的方法
该文章转载自德仔工作室:http://www.dezai.cn/Article_Show.asp?ArticleID=15031

whut0209 2007-10-24
  • 打赏
  • 举报
回复
自己 再顶!!!!!
whut0209 2007-10-24
  • 打赏
  • 举报
回复
AllowSorting="True"
也要设为false吧。


这句话是允许排序啊,和分页有关吗?

我在前台必须实现分页和排序的功能,但输出为Excel时要求全部输出。

哪个好心的高手帮帮忙啊!!
fs99 2007-10-24
  • 打赏
  • 举报
回复
AllowSorting="True"
也要设为false吧。
whut0209 2007-10-24
  • 打赏
  • 举报
回复
没人回答吗?白慕兄给掌掌眼啊!

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧