web project.
场景:
想把datagrid 中的数据导出到excel , 此datagrid是分页显示的。
Code
Response.Clear();
Response.AddHeader( " content-disposition " , " attachment;filename=Report.xls " );
Response.Charset = "" ;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = " application/vnd.xls " ;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this .DataGrid1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Response.AddHeader( " content-disposition " , " attachment;filename=Report.xls " );
Response.Charset = "" ;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = " application/vnd.xls " ;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this .DataGrid1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
测试结果:
报 Control 'dgList_01' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.
原因:
dgList 作了分页, 使用上面的方法不能使用 linkbutton 等对象, 分页功能默认使用了linkbutton对象。
修改:
就不说,太简单。
总结:
好长时间不写代码, 竟然这些地方都有些忘了。 还是要多看看msdn.