例如:
Dim cnFrom As New ADODB.Connection
Dim cnTo As New ADODB.Connection
Dim rsFrom As New ADODB.Recordset
Dim rsTo As New ADODB.Recordset
Dim rsTablesInExcel As New ADODB.Recordset
Dim strArryTableNameInExcel() As String
Dim intCounts As Integer
Dim intMaxArry As Integer
'连接Excel
cnFrom.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\source.xls;Extended Properties=Excel 8.0;Persist Security Info=False"
'连接SqlServer
cnTo.Open "DRIVER={SQL SERVER};SERVER=ServerName;Uid=sa;Pwd=Password;database=DatabaseName"
'得到打开的Excel文件中的用户表、存入数组
Set rsTablesInExcel = cnSource.OpenSchema(adSchemaTables)
If Not rsTablesInExcel.EOF Then
ReDim strArryTableNameInExcel(0)
For intCounts = 0 To rsTablesInExcel.RecordCount - 1
If UCase(rsTablesInExcel!TABLE_TYPE) = "TABLE" Then
intMaxArry = intMaxArry + 1
ReDim Preserve strArryTableNameInExcel(intMaxArry)
strArryTableNameInExcel(intMaxArry) = rsTablesInExcel!TABLE_NAME
End If
rsTablesInExcel.MoveNext
Next intCounts
End If
rsTablesInExcel.Close
Set rsTablesInExcel = Nothing
'循环到入打开的表
For intCounts = 1 To UBound(strArryTableNameInExcel)
rsFrom.Open "select * from [" & strArryTableNameInExcel(intCounts) & "]", cnFrom
rsTo.Open "select * from 目的表 where 1=2", cnTo
Do While Not rsFrom.EOF
rsTo.AddNew
rsTo.Fields(0) = rsFrom.Fields(0)
'.....各字段映射
rsTo.Update
Loop
rsFrom.Close
rsTo.Close
Next intCounts
Set rsFrom = Nothing
Set rsTo = Nothing
cnFrom.Close
cnTo.Close
Set cnFrom = Nothing
Set cnTo = Nothing
Dim excel_app As Object
Dim excel_sheet As Object
Dim rs As ADODB.Recordset
Dim strsql As String
Dim pubconn As ADODB.Connection
Dim exfieldA As String
Dim exfieldB As String
Dim exfieldC As String
Dim exfieldD As String
'打开数据库
Set rs = New ADODB.Recordset
Set pubconn = New ADODB.Connection
pubconn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=aaa;Data Source=(local)"
pubconn.Open
Set excel_app = CreateObject("excel.application") 'excel对象
Set excel_app = New Excel.Application
excel_app.Workbooks.Open FileName:="D:\***.xls"
If Val(excel_app.Application.Version) >= 8 Then '检查excel文件的版本
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If
'''创建sql表格
Dim crtstrsql As String
Dim exceltst As String