且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用VB.NET将所选列的数据从数据库导出到Excel?

更新时间:2023-02-07 09:02:18

引用:

我尝试了下面的代码,但它导出了所有列。



可能是因为这个

 sql =   SELECT * FROM publicationinfo 



请求数据库中的所有列。

要么你有要说明在SQL查询中需要哪些列,要么在写入Excel时必须对它们进行过滤。



SQL SELECT语句 [ ^ ]


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.

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

Quote:

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


Probably because this

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[^]