能否用VB写个程序,从EXCEL表中导入数据到SQL中指定的表的字段里,EXCEL表中有两列对应SQL表中有两个字段,谢谢

解决方案 »

  1.   

    我自己写的,速度不快,但还稳定,你自己改一下就是你要的了。
    Public Sub ExceltoSQL(ExcelSheetName As String, ExcelPath As String, SqlTablename As String, SqlDatabasename As String)
    MainForm.List1.MousePointer = 11
    Dim RsExtoSQL As New ADODB.Recordset
    Dim CnExtoSql As New ADODB.ConnectionDim xlBook As Excel.Workbook
    Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
    Set xlBook = xlApp.Workbooks.Open(ExcelPath) '打开已经存在的EXCEL工件簿文件
    xlApp.Worksheets(ExcelSheetName).Activate
    Dim i As Integer
    Dim MM As Integer
    Dim nn As Integer
    Dim jj As Integer
    Dim Trans As String
    i = 1
    Dim Fieldname() As Variant
    ReDim Fieldname(0) As VariantCnExtoSql.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=chang;PWD=assetok;Initial Catalog=" & SqlDatabasename & ";Data Source=192.168.0.250"
    CnExtoSql.Open
    Do While xlApp.Cells(1, i).Value <> "" And Not IsNull(xlApp.Cells(1, i).Value)
       If Fieldname(UBound(Fieldname)) <> "" Then ReDim Preserve Fieldname(UBound(Fieldname) + 1)
       Fieldname(UBound(Fieldname)) = Replace(xlApp.Cells(1, i).Value, "&pound;¨", "(")
       Fieldname(UBound(Fieldname)) = Replace(Fieldname(UBound(Fieldname)), "&pound;&copy;", ")")
       i = i + 1
    Loop
    Dim strSQL As String
    Dim nno As Integer
    For nno = 0 To UBound(Fieldname)
       strSQL = strSQL & "[" & Fieldname(nno) & "] text not null,"
    Next
    On Error Resume Next
    CnExtoSql.Execute "drop table [" & SqlTablename & "]"
    On Error GoTo 0
    CnExtoSql.Execute "create table [" & SqlTablename & "] ( " & Left(strSQL, Len(strSQL) - 1) & ")"RsExtoSQL.Open "select * from " & SqlTablename, CnExtoSql, adOpenDynamic, adLockOptimistic
    MM = 2 '&ETH;&ETH;
    nn = 1 '&Aacute;&ETH;
    Do While ExtoAcCheckTable(MM, i - 1)
    RsExtoSQL.AddNew
        Do While nn <> i
            If xlApp.Cells(MM, nn).Value = "" Or IsNull(xlApp.Cells(MM, nn).Value) Then
                xlApp.Cells(MM, nn).Value = "?"
            End If
            Trans = CStr(xlApp.Cells(MM, nn).Value)
            If Left(Trans, 1) = "." Then
                RsExtoSQL.Fields.Item(nn - 1).Value = "0" & Trans
            Else
            RsExtoSQL.Fields.Item(nn - 1).Value = Replace(Trans, "&pound;¨", "(")
            RsExtoSQL.Fields.Item(nn - 1).Value = Replace(RsExtoSQL.Fields.Item(nn - 1).Value, "&pound;&copy;", ")")
            End If
    '        RsExtoSQL.Fields.Item(nn - 1).Value = xlApp.Cells(MM, nn).Value
        nn = nn + 1
        Loop
    MM = MM + 1
    nn = 1
    Loop
    RsExtoSQL.Update
    Set xlApp = Nothing
    xlBook.Close (False)
    If CnExtoSql.State <> adStateClosed Then CnExtoSql.Close
    MainForm.List1.MousePointer = 0
    End Sub
    Private Function ExtoAcCheckTable(NumberRow As Integer, NumberCol As Integer) As Boolean
       Dim ab As Integer, ac As Integer, YesNo As Boolean
       YesNo = False
       For ab = 0 To 5 '判断后5行是否有值
           For ac = 1 To NumberCol
               If xlApp.Cells(NumberRow, ac).Value <> "" And Not IsNull(xlApp.Cells(NumberRow, ac).Value) Then
                   YesNo = True
                   GoTo Wanle
               End If
           Next
          NumberRow = NumberRow + 1
       Next
    Wanle:
    If YesNo = True Then ExtoAcCheckTable = True
    If YesNo = False Then ExtoAcCheckTable = False
    End Function
      

  2.   

    另外还有一种方法:
    将Excel的数据导入SQL server :
    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]实例:
    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
      

  3.   

    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
    这些代码写在那里执行,SQL还是VB中,还有,用的是SQL原来的表,只不过改它的两个字段的内容,其他不变,谢谢
      

  4.   


       放在一个COMMAND里面不就可以了!
      

  5.   

    放在COMMAND里还没运行就显示红色,语法错误啊!!!,怎回事????