【Vegas原创】VB.NET版Excel导出(GridView、DataGrid通吃)

在click事件中加入:

      Protected Sub imgExcel_Click(ByVal sender As ObjectByVal e As System.Web.UI.ImageClickEventArgs) Handles imgExcel.Click
            Response.Clear()
            Response.Buffer 
= True
            Response.Charset 
= "GB2312"
            Response.AppendHeader(
"Content-Disposition""attachment;filename=FileName.xls")
            
'如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!

            Response.ContentEncoding 
= System.Text.Encoding.UTF7
            Response.ContentType 
= "application/ms-excel" '/设置输出文件类型为excel文件。 

            
Dim oStringWriter As New System.IO.StringWriter()
            
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
            
'  turn off paging 
            grdeap.AllowPaging = False
            grdeap.Columns(
0).Visible = False
            
'注意:不是grdeap.databind()
            mydatabind()
            grdeap.RenderControl(oHtmlTextWriter)
            Response.Output.Write(oStringWriter.ToString())
            Response.Flush()
            Response.End()

        
End Sub


 

原文地址:https://www.cnblogs.com/amadeuslee/p/3744574.html