Locked How to export data from SQL Server 2005 to Excel

  • Wednesday, November 02, 2011 5:50 AM
     
     
    Hi,
    My application is in VS2008 and coded in Vb.net.
    I have my database in Sql Server 2005.I have a Database table say Table1 in My Database named Test.
    Now i want to export the data in Table1 to a MS-Excel(.xls) file on the click of a button.
    An excel file should be generated after the user clicks on the button and the generated Excel file should have the data that is present in Table1.
    Can anyone provide me with a link or some helpful code to do this.
    My code is in Vb.net
    Thanks.

All Replies

  • Wednesday, December 21, 2011 12:13 PM
     
     

    Check below thread

    http://support.microsoft.com/kb/306125

  • Monday, December 26, 2011 12:53 PM
     
     Proposed Has Code

    Hi Sameer ..
    Welcomes you to MSDN forums..
    Code Snipt:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports Excel = Microsoft.Office.Interop.Excel
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim cnn As SqlConnection
            Dim connectionString As String
            Dim sql As String
    
            connectionString = "data source=servername;" & _
            "initial catalog=databasename;user id=username;password=password;"
            cnn = New SqlConnection(connectionString)
            cnn.Open()
            sql = "SELECT * FROM Product"
            Dim dscmd As New SqlDataAdapter(sql, cnn)
            Dim ds As New DataSet
            dscmd.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
            cnn.Close()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    
    
            Dim xlApp As Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value
            Dim i As Integer
            Dim j As Integer
    
            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            xlWorkSheet = xlWorkBook.Sheets("sheet1")
    
            For i = 0 To DataGridView1.RowCount - 2
                For j = 0 To DataGridView1.ColumnCount - 1
                    xlWorkSheet.Cells(i + 1, j + 1) = _
                        DataGridView1(j, i).Value.ToString()
                Next
            Next
    
            xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
            xlWorkBook.Close()
            xlApp.Quit()
    
            releaseObject(xlApp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)
    
            MsgBox("You can find the file C:\vbexcel.xlsx")
        End Sub
    
        Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
    End Class
    


    I hope it will helps to solve your requirements

    By
    A Pathfinder..
    JoSwa


    If you find an answer helpful, click the helpful button. If you find an answer to your question, mark it as the answer.
  • Thursday, February 02, 2012 9:22 AM
     
     

    Hello,

    Have a look at the links below. It will do your task.

    http://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/

    http://www.mssqltips.com/sqlservertip/1202/export-data-from-sql-server-to-excel/

     

    From asp.net code

    Get the data to a dataset from your table in your .net code and then go through the links below to export it.

    http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx

    http://forums.asp.net/t/1197704.aspx/1