下面是我用来将我的 gridview 数据导出到 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