Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have only one button on my windows form, a button that performs exporting of data from database to excel.
It works in exporting but all those columns in database have exported. I want to export only those specific/selected/desired columns. I don't want to export all columns. I want also to export selected columns from database to selected columns in excel. For example: column1, column4, and column 6 data of database to be exported to columns: A,C,D of excel. I hope you can help me. Thanks.

What I have tried:

I have tried the code below but it exporting all columns.

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dbSource As String
        Dim sql As String
        Dim sqlcon As New OleDb.OleDbConnection
        Dim ds As New DataSet
        Dim dbProvider As String
        Dim i, j As Integer

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        dbProvider = "Provider=Microsoft.ACE.OLEDB.15.0;"
        dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
        dbSource = "Data Source =psipop.accdb;Jet OLEDB:Database Password=cscfo13poppsi; "
        sqlcon.ConnectionString = dbProvider & dbSource

        sql = "SELECT * FROM publicationinfo"
        Dim dscmd = New OleDb.OleDbDataAdapter(sql, sqlcon)
        dscmd.Fill(ds)

        For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                xlWorkSheet.Cells(i + 18, j + 1) = ds.Tables(0).Rows(i).Item(j) '+18 here is the rownumber(i) and +1 is the columnnumber(j)
            Next
        Next

        xlWorkSheet.SaveAs("C:\Users\Admin\Desktop\vbexcel.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        sqlcon.Close()

        MsgBox("You can find the file C:\Users\Admin\Desktop\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
Posted
Updated 14-May-19 19:49pm

1 solution

Quote:
I have tried the code below but it exporting all columns.

Probably because this
VB
sql = "SELECT * FROM publicationinfo"

request all the columns from database.
Either you have to say which columns you want in the SQL query, either you have to filter them when you write to excel.

SQL SELECT Statement[^]
 
Share this answer
 
Comments
kyrons 15-May-19 4:19am    
Thanks..I did not notice the *(means all). Now I revised the sql statement to: sql = "SELECT column1, column4, column6 FROM publicationinfo" and it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900