本文介绍了将数据库从Gridview导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我测试了下面的代码将数据库从Gridview导出到Excel,但是错误 类型为'GridView'的控件'gvInHoaDon'必须放在带有runat = server的表单标签内 private void InHoaDonDuocChon(GridView gvInHoaDon) { Response.Clear(); Response.Buffer = true ; Response.AddHeader( content-disposition, attachment; filename = HoaDon.xls); Response.Charset = ; Response.ContentType = application / ms-excel; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); gvInHoaDon.AllowPaging = false ; gvInHoaDon.DataBind(); gvInHoaDon.RenderControl(hw); // ThayđổiStyle string style = @ < style> .textmode {mso-number-format:\ @ ;}< / style>; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } 我引用了两种方式: http://www.codedigest.com/Articles/ASPNET/130_Export_to_Excel_in_ASPNet_20_-Gridview_to_Excel_DataTable_to_Excel.aspx [ ^ ] 但错误类型'GridView'的控件'gvInHoaDon'必须放在带有runat = server的表单标签内 请帮帮我。 非常感谢。解决方案 I tested a below code to export database from Gridview to Excel,but error"Control 'gvInHoaDon' of type 'GridView' must be placed inside a form tag with runat=server"private void InHoaDonDuocChon(GridView gvInHoaDon) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=HoaDon.xls"); Response.Charset = ""; Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); gvInHoaDon.AllowPaging = false; gvInHoaDon.DataBind(); gvInHoaDon.RenderControl(hw); //Thay đổi Style string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); }I referenced 2 ways: http://www.codedigest.com/Articles/ASPNET/130_Export_to_Excel_in_ASPNet_20_-Gridview_to_Excel_DataTable_to_Excel.aspx[^]but error "Control 'gvInHoaDon' of type 'GridView' must be placed inside a form tag with runat=server"Please help me.Many thanks. 解决方案 这篇关于将数据库从Gridview导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 12:18