Private Sub mnuImport_Click() Dim strSource As String Dim Cn As New ADODB.Connection Dim Rs As New ADODB.Recordset comOpen.Filter = "报表文件(*.xls)|*.xls" comOpen.ShowOpen
If comOpen.FileName = "" Then Exit Sub Else strSource = comOpen.FileName strSource=inputbox$()
If strSource <> "" Then If strSource = "" Then MsgBox "请确定名字", vbInformation, "提示" Exit Sub ElseIf strSource = "sheet1" Or strSource = "sheet3" Then If Cn.State <> 1 Then Cn.CursorLocation = adUseClient Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;" _ & "Data Source=" & strSource & ";Extended Properties='Excel 8.0;HDR=Yes'" Rs.Open "select * from [" & strSource & "$]", Cn, adOpenDynamic, adLockOptimistic Set DataGrid1.DataSource = Rs DataGridColumnWidth MsgBox "文件导入成功", vbInformation, "提示"
DataGrid1.Visible = True ElseIf Cn.State = 1 Then MsgBox "已经有一个连接", vbCritical, "警告" Exit Sub End If Else MsgBox "名字错误", vbInformation, "提示" Exit Sub End If Else Exit Sub End If strSource = "" End IfEnd Sub
Dim strSource As String
Dim Cn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
comOpen.Filter = "报表文件(*.xls)|*.xls"
comOpen.ShowOpen
If comOpen.FileName = "" Then
Exit Sub
Else
strSource = comOpen.FileName
strSource=inputbox$()
If strSource <> "" Then
If strSource = "" Then
MsgBox "请确定名字", vbInformation, "提示"
Exit Sub
ElseIf strSource = "sheet1" Or strSource = "sheet3" Then
If Cn.State <> 1 Then
Cn.CursorLocation = adUseClient
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;" _
& "Data Source=" & strSource & ";Extended Properties='Excel 8.0;HDR=Yes'"
Rs.Open "select * from [" & strSource & "$]", Cn, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = Rs
DataGridColumnWidth
MsgBox "文件导入成功", vbInformation, "提示"
DataGrid1.Visible = True
ElseIf Cn.State = 1 Then
MsgBox "已经有一个连接", vbCritical, "警告"
Exit Sub
End If
Else
MsgBox "名字错误", vbInformation, "提示"
Exit Sub
End If
Else
Exit Sub
End If
strSource = "" End IfEnd Sub