Protected Sub ButExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' 下傳成Excel File 注意,換頁模式啟用時不能下傳成Excel File,
故先將GV的換頁模式改為FALSE後,再重LOAD一次GridView即可下傳
me.GVQuery.AllowPaging=False
BindGridView() ( 是用來做sql查詢及與GridView Bind起來的副程式)
## 下列程式照寫即可 ##
Response.Clear()
' 下傳的檔名預設為FileName.xls,也 可以異動哦
Response.AppendHeader("Content-Disposition", "attachment; filename=FileName.xls")
Response.Charset = "big5"
Response.ContentType = "application/vnd.xls"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
Response.Write("")
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8")
出現亂碼時要加上這句話即可
GVQuery.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.End()
'再將原來GridVeiw的換頁模式啟用,再重LOAD一次GridView即可下傳
GVQuery.AllowPaging=True
BindGridView()
End Sub
### 下列的sub必須存在才行 ##
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
'處理'GridView' 的控制項'GridView' 必須置於有runat=server 的表單標記之中
End Sub
'再將原來GridVeiw的換頁模式啟用,再重LOAD一次GridView即可下傳
GVQuery.AllowPaging=True BindGridView()
