GridView匯出轉成Excel
可以下面的code就可以匯出excel
如果設定好後又出現以下的錯誤
那要在*.aspx 的header上<%@加入
可以下面的code就可以匯出excel
Protected Sub BtnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnExcel.Click
Dim strExportFilename As String = "ExportedData"
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=" + strExportFilename + ".xls")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Response.Charset = "big5"
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As New HtmlTextWriter(stringWrite)
GridView1.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString().Replace("<div>", "").Replace("</div>", ""))
Response.End()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
'解決Error:型別 'GridView' 的控制項 'GridView1' 必須置於有 runat=server 的表單標記之中。
End Sub
如果設定好後又出現以下的錯誤
RegisterForEventValidation 只能在 Render(); 期間呼叫
那要在*.aspx 的header上<%@加入
EnableEventValidation = "false" AutoEventWireup="true"
留言