There are many ways to export data from SQL Server to Excel but I think the one way to do it by real simple coding is to export the data as an XML file and then opening it in Excel.

First we fill the data in a DataSet and then export that data to an XML file. Although the file is an XML one, I give it an extension of ‘.xls’ so that Excel opens it by default and then it can be saved it as the Excel format in some other sheet.

Dim con as SqlConnection
con = New SqlConnection(“Data Source=SERVER; Initial Catalog=dbTest;uid=sa;pwd=123″)
Dim cmd As SqlCommand = New SqlCommand
con.Open()
cmd.CommandText = “spExportData”
cmd.CommandType = CommandType.StoredProcedure

cmd.Connection = con
Dim ds As New DataSet
Dim da As New SqlDataAdapter
da.SelectCommand = cmd
da.Fill(ds)
Dim savefileName As String = Application.StartupPath & “\ExportedFile.xls”
ds.WriteXml(savefileName)
con.Close()
cmd.Dispose()
da.Dispose()

  • 1 Comment
  • Filed under: .NET, Programming
  • Use Ajax and PHP to Build a Chat Application

    Jack D. Herrington (a senior software engineer with more than 20 years of experience) writes in detail on how to build a chat system on a web application using Ajax, XML and PHP.

  • 2 Comments
  • Filed under: Programming
  • About



    Hello, I am Pushkar Singh Arora. I am currently a Computer Science student and I love to learn new technologies, specially in software programming.

    This Blog is my portal to online world where I put in my thoughts, Mac tips, exciting new things I learn (or already learned), links to articles I find interesting as well as lots of random stuff.


    Home Office Furniture with Free Shipping


    I now work for Wholesale Furniture Brokers as a programmer and I highly recommend that you check out our selection of home office furniture if you are looking for office chairs, filing cabinets, or even computer desks. We offer free shipping on everything and I’ve actually seen the furniture in person and the quality is better than I imagined.