导入excel文件并保存在mysql数据库中

时间:2014-02-11 07:34:48

标签: mysql excel-vba vba excel

我在数据库中上传excel文件时遇到问题...

我正在寻找一个代码,允许我的项目浏览excel文件,然后将所有数据保存在我的数据库中,并自动将其显示在我的数据网格视图中。

我已经搜索了大约两天的代码,我已经尝试过其中的一些但仍然没有找到适合我的问题的代码..

我尝试了这段代码..但是效果不好。

Dim conko As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

Try
    Dim fBrowse As New OpenFileDialog
    With fBrowse
        .Filter = "Excel files(*.xls)|*.xls|All files (*.*)|*.*"
        .FilterIndex = 1
        .Title = "Import data from Excel file"
    End With
    If fBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim fname As String
        fname = fBrowse.FileName
        Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source= " & fname & ";" & " Extended Properties= ""Excel 8.0;HDR=NO;IMEX=1"""
        Dim Connection As New System.Data.OleDb.OleDbConnection(ConnectionString)
        Connection.Open()

        Dim CollectionName As String = "Tables"
        Dim dt As DataTable = Connection.GetSchema(CollectionName)

        Dim TableRow As DataRow
        For Each TableRow In dt.Rows
            If TableRow.Item("userlog").ToString.IndexOf("$") = TableRow.Item("userlog").ToString.Length - 1 Then
                Console.WriteLine(TableRow.Item("userlog").ToString)
            End If
        Next

        Connection.Close()

    End If
Catch ex As Exception
    MsgBox(ex.Message)
End Try