下面是我用來將我的gridview數據導出到Excel的方法。用戶詢問我是否可以命名工作表選項卡。有任何想法嗎?提前致謝!VB ASP.Net導出到Excel:名稱工作表選項卡
Private Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
Dim form As New HtmlForm
Dim strAttachment As String
Dim stw As StringWriter
Dim htextw As HtmlTextWriter
stw = New StringWriter
strAttachment = "attachment; filename=" & strAppName & ".xls"
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("content-disposition", strAttachment)
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
htextw = New HtmlTextWriter(stw)
form.Controls.Add(CType(Session("gridViewControl"), Control))
Me.Controls.Add(form)
form.RenderControl(htextw)
Response.Write("<b>" & txtTitle.Text & "</b><br />")
Response.Write(stw.ToString())
Response.Flush()
Response.Close()
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub