一个datagrid转为excel的问题
点击按钮后,转向处理excel程序,
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Redirect("devicegrouplist_print.aspx?bExcel=1");
}
if(Request.QueryString["bExcel"] !=null)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
//关闭 ViewState
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
dvdatagrid.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Response.End();
}
发生如下错误:!!!!!!!!!!!!!!!!!!!!!!!!
System.Web.HttpException: 类型“DataGridLinkButton”的控件“dvdatagrid__ctl3__ctl0”必须放在具有 runat=server 的窗体标记内。
源错误:
行 114: //此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
行 115: //获取control的HTML
行 116: dvdatagrid.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
行 117: // 把HTML写回浏览器
行 118: Response.Write(tw.ToString());
大侠依次为帮分析分析
问题点数:0、回复次数:2Top
1 楼JeasonZF(哭泣的鱼)回复于 2005-06-01 15:00:09 得分 0
你这种方法不太好,如果datagrid有分页、排序等就会出错
还是用下面这种好:
。。。。。。//以上省略(就是取纪录的代码)
DataTable dt=ds.Tables["table1"];
StringWriter sw=new StringWriter();
sw.WriteLine("自动编号,姓名,年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();Top
2 楼billlyh9()回复于 2005-06-03 08:37:04 得分 0
JeasonZF(哭泣的鱼)
你这个是保存为execel呀,我要的是,一点击 页面就转为execel文件的页面Top




