Private Sub ToExcel()
'定義導出的EXCEL名
Dim rnd As Random
rnd = New Random
Dim excelName As String
excelName = DateTime.Now.ToString("yyyymmddhhmmss_") + rnd.Next(1000).ToString + ".xls"
Response.Clear()
Response.Buffer = True
Response.Charset = "utf-8"
Response.AppendHeader("content-disposition", "attachment;filename=" + excelName)
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8")
Response.ContentType = "application/ms-excel"
Me.EnableViewState = False
Dim sw As System.IO.StringWriter
sw = New System.IO.StringWriter
Dim hw As System.Web.UI.HtmlTextWriter
hw = New System.Web.UI.HtmlTextWriter(sw)
me.RenderControl(hw)
'me為要導出的控件名稱
Response.Write(sw.ToString)
Response.End()
End Sub