【VB.net】MYSQL导出至Excel

Imports System.Data
Imports Microsoft.SqlServer.Server
Imports MySql.Data.MySqlClient
Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    'MYSQL数据导出至EXCEL

    Private Sub 导出ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 导出ToolStripMenuItem1.Click
        Try
            '创建连接
            '  Dim cnn As dataaccess = New dataaccess(connection_string)
            Dim cnn As String = "Server=localhost;User ID=root;Password=;Database=emp;"
            Dim i, j As Integer
            '创建Excel对象
            Dim xlApp As Microsoft.Office.Interop.Excel.Application
            Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
            Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value
            xlApp = New Microsoft.Office.Interop.Excel.Application
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            ' 打开某一个表单
            xlWorkSheet = xlWorkBook.Sheets("sheet1")
            ' sql查询
            '  xlWorkBook.Sheets.Select("A1:A2")

            Dim sql As String = "SELECT * FROM EMP"
            ' SqlAdapter
            Dim dscmd As New MySqlDataAdapter(sql, cnn)

            ' 定义数据集
            Dim ds As New DataSet
            dscmd.Fill(ds)
            '添加字段信息到Excel表的第一行
            xlWorkSheet.Cells(1, 1).Value = "empno"
            xlWorkSheet.Cells(1, 2).Value = "username"
            xlWorkSheet.Cells(1, 3).Value = "password"
            xlWorkSheet.Cells(1, 4).Value = "sex"
            xlWorkSheet.Cells(1, 5).Value = "sal"
            xlWorkSheet.Cells(1, 6).Value = "tel"
            xlWorkSheet.Cells(1, 7).Value = "hiredate"
            xlWorkSheet.Cells(1, 8).Value = "address"
            xlWorkSheet.Cells(1, 9).Value = "role"
            ' 将数据导入到excel
            For i = 0 To ds.Tables(0).Rows.Count - 1
                'Column

                For j = 0 To ds.Tables(0).Columns.Count - 1
                    ' this i change to header line cells >>>
                    xlWorkSheet.Cells(i + 3, j + 1) = _
                    ds.Tables(0).Rows(i).Item(j)
                Next
            Next
            'HardCode in Excel sheet
            ' this i change to footer line cells  >>>
            'xlWorkSheet.Cells(i + 3, 7) = "Total"
            ' xlWorkSheet.Cells.Item(i + 3, 8) = "=SUM(H2:H18)"
            ' 保存到Excel
            xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
            xlWorkBook.Close()
            xlApp.Quit()
            releaseObject(xlApp)
            releaseObject(xlWorkBook)
            releaseObject(xlWorkSheet)
            '弹出对话框显示保存后的路径
            MsgBox("You can find the file D:\vbexcel.xlsx")
        Catch ex As Exception

        End Try
    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



 

你可能感兴趣的:(VB.net,MySql,Excel)