Here's a quick snippet that will export a datagrid's contents to Microsoft Excel. This assumes that you have a button named btnExport on your webform and of course Excel installed on your pc.
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim sw As New StringWriter
Dim hw As New HtmlTextWriter(sw)
Try
Response.Clear()
Response.AddHeader("content-disposition", "attachment; filename=test.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Me.dgMain.RenderControl(hw)
Response.Write(sw.ToString)
Response.End()
Catch ex As Exception
Me.lblMsg.Text = ex.Message
Finally
sw = Nothing
hw = Nothing
End Try
End Sub
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment