标题:vb中将excel导入SQL的问题
取消只看楼主
mhjuan119
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-10-25
结帖率:0
已结贴  问题点数:10 回复次数:1 
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 编辑 ]
搜索更多相关主题的帖子: excel表格 SQL数据库 
2015-03-30 15:23
mhjuan119
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-10-25
得分:0 
如何才能导入任何表,excel表中的表头和sql表中的字段是完全对应的。
2015-03-30 16:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-443276-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.059375 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved