Export to CSV
Wednesday 02 February 2011
If you have a requirement to export to Excel, it's worth considering CSV, as unfortunately a lot of people still don't seem to have the ability to open Excel spreadsheets.
To export to CSV using asp.net (VB.net), just use the below code:
- Dim MyConnection As SqlConnection
- MyConnection = New SqlConnection()
- MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
- Dim sql As String = "SELECT Column1, Column2, etc FROM TABLENAME"
- Dim resultsDataSet As New DataSet()
- Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(Sql, MyConnection)
- Dim dataTable As DataTable = resultsDataSet.Tables(0)
- Response.ContentType = "Application/x-msexcel"
- Response.AddHeader("content-disposition", "attachment;filename=filename.csv")