protected void Button1_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Productor.doc");
Response.ContentType = "application/msword";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindData();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
GridView1.AllowPaging = true;
}
这段程序所要实现的功能我清楚,但是很多具体实现的细节不理解,从StringWriter sw = new StringWriter();这一行开始我就晕了。。请哪位好心人帮我推敲推敲,不胜感激!BindData()是将GridView与数据库绑定的函数,如下:
private void BindData()
{
String strConnection = "provider=Microsoft.Jet.OleDb.4.0;data source=F:\\ASPXROOT\\WebSite1\\App_Data\\Northwind.mdb;";
OleDbConnection objConnection = new OleDbConnection(strConnection);
String strSQL = "SELECT * FROM 产品";
OleDbCommand objCommand = new OleDbCommand(strSQL,objConnection);
objConnection.Open();
GridView1.DataSource = objCommand.ExecuteReader();
GridView1.DataBind();
objConnection.Close();
}
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=Productor.doc");
Response.ContentType = "application/msword";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindData();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
GridView1.AllowPaging = true;
}
这段程序所要实现的功能我清楚,但是很多具体实现的细节不理解,从StringWriter sw = new StringWriter();这一行开始我就晕了。。请哪位好心人帮我推敲推敲,不胜感激!BindData()是将GridView与数据库绑定的函数,如下:
private void BindData()
{
String strConnection = "provider=Microsoft.Jet.OleDb.4.0;data source=F:\\ASPXROOT\\WebSite1\\App_Data\\Northwind.mdb;";
OleDbConnection objConnection = new OleDbConnection(strConnection);
String strSQL = "SELECT * FROM 产品";
OleDbCommand objCommand = new OleDbCommand(strSQL,objConnection);
objConnection.Open();
GridView1.DataSource = objCommand.ExecuteReader();
GridView1.DataBind();
objConnection.Close();
}
解决方案 »
- 请教sql中过多的使用 or 是否会影响性能
- table嵌套样式被撑大
- 类似论坛更新评论的问题
- 用sql如何求每年每月有多少天
- 奇怪的“automation服务器不能创建对象”,通用方法不能解决。
- 请教request的用法~~~求值!!!
- 怎样将GridView中的TextBox值绑定为SqlDataSource的Update参数?
- 我想用C#在服务器端对XML,XSLT解析,得到XHTML,可是它总是自动加入一些代码,导致无法通过W3C验证,怎么解决?
- TreeView的问题(asp.net C#)
- 这次死大了,好好的发邮件程序怎么就不能用了?????
- JavaScript直接调用后台的WCF服务进行数据的增删改查Demo 要的赶紧下载
- 多个web.config的问题
再设置GridView1分页 前台要展现分页的。 这种是最山寨的导出word方法。
在导出gridview 时要取消分页 等导出结束后 要恢复 gridview 分页,要不页面中的gridview 显示数据时就不分页了
//由于页面Gridview有分页 所以要 取消分页 重新绑定GridView 这要导出的才是 GridView中的所有内容 GridView1.AllowPaging = false;
BindData();//将gridview中的内容输入到所提供的 htw 对象中 GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
//要重新恢复分页 要不页面中的GridView 显示的内容将 不分页 GridView1.AllowPaging = true;
//由于页面Gridview有分页 所以要 取消分页 重新绑定GridView 这要导出的才是 GridView中的所有内容 GridView1.AllowPaging = false;
BindData();//将gridview中的内容输入到所提供的 htw 对象中
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
//要重新恢复分页 要不页面中的GridView 显示的内容将 不分页
GridView1.AllowPaging = true;
//新建一个 将文本写入到服务器控件输出流的 一个对象,将 htw里面的内容写入到 sw 对象中HtmlTextWriter htw = new HtmlTextWriter(sw);
我怎么感觉是将sw的内容写入到htw里啊,求解释