Code (VB.NET)
Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
'Response.Charset = "Default"
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
GridView1.RenderControl(oHtmlTextWriter)
Response.ContentEncoding = System.Text.Encoding.UTF8
' Set ไทย ให้เครื่อง server ใช้ .UTF7 แต่ถ้าเครื่องเราใช้ UTF8'
Response.Write(oStringWriter.ToString())
Response.[End]()
End Sub
Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
ExportToExcel("Report.xls", GridView1)