自己一直就是使用Devexpress控件做的导出数据到Excel,今天做了一个asp控件的导出数据到Excel中,下面利用一点点时间给大家分享一下,也许网上很多源码,但是我觉得毕竟是自己手敲出来的,价值更值得和大家分享:首先,我把前台的设计给贴出来:
在这说一下,我使用的是SqlDataSource作的数据源。其次:一下是后台代码: protected void Button1_Click1(object sender, EventArgs e) { GridView1.Columns[0].Visible = false; //隐藏删除 GridView1.Columns[1].Visible = false; //隐藏编辑 StringWriter sw = new StringWriter(); HtmlTextWriter htmlwrite = new HtmlTextWriter(sw); string excelName = "个人信息.xls"; Response.ContentType = "application/x-excel"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(excelName)); GridView1.RenderControl(htmlwrite); Response.Write(sw.ToString()); Response.Flush(); Response.End(); } //此方法不能删除,如果没有下面方法会报错类型“GridView”的控件"ct100_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内 public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for //如果要限制控件必须位于