我在数据库中上传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
答案 0 :(得分:0)
也许您可以引用此PHP代码并查看它在VBA中的工作原理:
Read excel file and store it to mysql database
好的,我找到了其他一些链接:
http://www.vbaexpress.com/forum/showthread.php?46880-vba-code-import-table-excel-to-mysql
http://forums.mysql.com/read.php?10,100302
http://www.mrexcel.com/forum/excel-questions/734639-mysql-excel-visual-basic-applications.html