Export to CSV




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:
  1. Dim MyConnection As SqlConnection   
  2.         MyConnection = New SqlConnection()   
  3.         MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString   
  4.   
  5.         Dim sql As String = "SELECT Column1, Column2, etc FROM TABLENAME"  
  6.   
  7.         Dim resultsDataSet As New DataSet()   
  8.         Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(Sql, MyConnection)   
  9.         myDataAdapter.Fill(resultsDataSet)   
  10.         Dim dataTable As DataTable = resultsDataSet.Tables(0)   
  11.   
  12.         Response.ContentType = "Application/x-msexcel"  
  13.         Response.AddHeader("content-disposition""attachment;filename=filename.csv")   
  14.         Response.Write(ToCSV(dataTable))   
  15.         Response.[End]()  
Where you see the connection string information above, this will need to match that which is in your web.config file. If you're not familiar with this, please take a look at SQL Connection Strings in asp.net 2.0 and above.


No one has said that they like article yet, be the first and click here (0)
Send a link to this page via email Send a link to this page