Export To Excel

This code will show you that how you can export your data to Excel.


Web.Config



------------------------------------------------------------------------
ExportToExcel.aspx.vb

Imports System.Data
Imports System.Data.SqlClient
Partial Class ExportToExcel
Inherits System.Web.UI.Page


Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExportExcel.Click
Dim conn As SqlConnection
conn = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnString").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand("select * from employees", conn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
BindExcel(ds)
conn.Close()
End Sub
Sub BindExcel(ByVal ds As DataSet)

Response.Clear()
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWrite)
Dim dg As System.Web.UI.WebControls.DataGrid = New System.Web.UI.WebControls.DataGrid()
dg.DataSource = ds.Tables(0)
dg.DataBind()
dg.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
End Sub
End Class


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: