将gridview 导出到excel

在前台加个按钮 Btn_ExportExcel,然后在后台加上以下的函数

 

Protected Sub Btn_ExportExcel_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_ExportExcel.Click                          Response.Charset = "utf-8"

        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")

 

        Response.ClearContent()

        Response.AddHeader("content-disposition", "attachment; filename=" & System.Web.HttpUtility.UrlEncode("new", System.Text.Encoding.UTF8) & ".xls")

        Response.ContentType = "application/excel"

 

        Dim sw As New StringWriter

        Dim htw As New HtmlTextWriter(sw)

       //此句中指定 gridview 控件

        Main_grdv.RenderControl(htw)

         Response.Write(sw.ToString)

        Response.End()    

End

//不加此函数会报错

Sub     Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

posted @ 2012-06-11 10:20  withoutaword  阅读(148)  评论(0编辑  收藏  举报