vb中将excel表导入到sql中相同结构的数据表中
下面是导入按钮COMMAND1_CLICK里面的代码
text4为显示读取文件路径的。
If Trim(Text4.Text) = "" Then
MsgBox "请选择导入的路径!", vbInformation, Me.Caption
Text4.SetFocus
Exit Sub
End If
If MsgBox("确实要导入信息吗?", vbQuestion + vbYesNo, Me.Caption) = vbNo Then
Exit Sub
End If
MousePointer = 11
'EXCEL导入
Call ImportZJZHXX(Trim(Text4.Text & "\文件名.xls"))
===========================
以下是导入的方法
'通过Excel导入数据库中
Private Function ImportZJZHXX(ByVal strExcelFile As String) As Boolean
Dim ExcelApp As Excel.Application
Dim ExcelSheet As Excel.Worksheet
Dim sql As String
Dim Hzrs As New ADODB.Recordset
Dim gcnData As ADODB.Connection
Dim icount1 As Integer
Dim i As Integer
gcnData.Open "Provider=MSDAORA.1;Password=dzcs;User ID=dzcs;Data Source=ORCL_72.11"
On Error GoTo ErrHandler
If strExcelFile = vbNullString Then
MsgBox "没有Excel文件!", vbInformation, Me.Caption
Exit Function
End If
Me.MousePointer = vbHourglass
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Workbooks.Open FileName:=strExcelFile
Set ExcelSheet = ExcelApp.ActiveSheet
If ExcelSheet.UsedRange.Rows.Count > 1 Then
Hzrs.Open "select * from JjHkXx where 1=2", gcnData, adOpenKeyset, adLockOptimistic
For i = 2 To ExcelSheet.UsedRange.Rows.Count
Hzrs.AddNew
Hzrs!fsetcode = Trim(ExcelSheet.Cells(i, 1))
Hzrs!FskDw = Trim(ExcelSheet.Cells(i, 2))
Hzrs!FSkRen = Trim(ExcelSheet.Cells(i, 4))
Hzrs!fskbank = Trim(ExcelSheet.Cells(i, 5))
Hzrs.Update
icount1 = icount1 + 1
Next
Hzrs.Close
Else
Me.MousePointer = vbDefault
MsgBox "Excel文件中没有数据,请检查!", vbOKOnly + vbInformation, Me.Caption
Exit Function
End If
ExcelApp.ActiveWorkbook.Close False
ExcelApp.Quit
Set ExcelSheet = Nothing
Set ExcelApp = Nothing
MsgBox "导入信息成功,总行数 :" & icount1, vbOKOnly + vbInformation, Me.Caption
Me.MousePointer = vbDefault
ImportZJZHXX = True
Exit Function
ErrHandler:
If err.Number <> 0 Then
MsgBox "导入信息失败,原因可能是:" & err.Description, vbOKOnly + vbInformation, Me.Caption
Me.MousePointer = vbDefault
ImportZJZHXX = False
End If
End Function