vb中将excel导入SQL的问题
在vb中将excel导入SQL数据库,excel表格简单的话能顺利的导入,如果excel表格复杂一点,就只能导入一部分,是怎么回事?这是导入的代码。请高手指点!急急急!!!Private Sub DImport_Click()
On Error Resume Next
Dim filepath As String
Dim filename As String
Dim str0 As String
CommonDialog1.ShowOpen
filepath = CommonDialog1.InitDir
filename = CommonDialog1.filename
str0 = Mid(filename, InStrRev(filename, "\") + 1, Len(filename))
If MsgBox("你真的要导入 " & str0 & " 中的数据吗?", vbYesNo) <> vbYes Then Exit Sub
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim str As String
Dim cn1 As New ADODB.Connection
Dim cmd As New
Dim rs1 As New ADODB.Recordset
cn.ConnectionString = "driver={microsoft excel driver (*.xls)};dbq=" & filepath & filename
cn.Open
rs.Open "select * from [Sheet1$] ", cn, adOpenKeyset, adLockOptimistic’选择要导入excel表格中的sheet1工作表
cn1.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=MHJ\SQL2005"’设定sql数据库
cn1.Open
rs1.Open "select * from Wirerack", cn1, adOpenKeyset, adLockOptimistic’选择数据库test中的wirerack表
= "delete from Wirerack" '导入之前先清空原数据表
cn1.Execute
rs.MoveFirst
ProgressBar1.Max = rs.RecordCount
ProgressBar1.Value = 1
For j = 1 To rs.RecordCount
If rs.EOF = True Then
Exit Sub
Else
DoEvents
rs1.AddNew
For i = 0 To rs.Fields.Count - 1
rs1.Fields(i) = rs.Fields(i)
Next i
rs.MoveNext
End If
ProgressBar1.Value = j
Label2.Caption = Int(j / rs.RecordCount * 100) & "%"
Next j
rs1.Update
cn.Close
cn1.Close
MsgBox "导入数据成功!", , "提示窗口"
ProgressBar1.Value = 0
Label2.Caption = ""
Call Form_Load
End Sub
[ 本帖最后由 mhjuan119 于 2015-3-30 15:54 编辑 ]