VB如何获取excel工作表的名字
vb.net吧
全部回复
仅看楼主
level 7
暴牙归来 楼主
2016年06月26日 12点06分 1
level 7
暴牙归来 楼主
2016年06月26日 12点06分 2
level 13
工作表有名字?不是文件名吗?
---贴吧极速版 For UWP
2016年06月26日 12点06分 3
工作表指的不就是sheet1 、sheet2 、sheet3吗??
2016年06月26日 12点06分
回复 暴牙归来 : 这种问题你用对象浏览器就能得到答案 ---贴吧极速版 For UWP
2016年06月26日 13点06分
level 11
你指的是sheet1 2 3那些吗
2016年06月26日 12点06分 4
是啊
2016年06月26日 12点06分
你不是会vb6吗,给你个vb6的代码,你改哈
2016年06月26日 12点06分
@暴牙归来 你平时写的代码,跟vb6.0长得一样一样的[哈哈] 代码看楼下
2016年06月26日 13点06分
level 11
Private Sub Command3_Click()
On Error GoTo MyError
Dim cat As Object '数据库对象
Dim cn As Object '数据连接对象
Dim tbl As Object '数据表对象
Dim xlssql As String
Set cn = CreateObject("ADODB.Connection")
Set cat = CreateObject("ADOX.Catalog")
SheetName = Combo1.Text
If Len(SheetName) = 0 Then
MsgBox "请输入工作表(Sheet)名称", vbInformation, "温馨提示"
Exit Sub
End If
With cdlg
.Filter = "Excel 97-2003工作簿(*.xls)|*.xls|Excel 工作簿(*.xlsx)|*.xlsx"
.DialogTitle = "选择导入文件"
.InitDir = App.Path
.FileName = ""
.ShowOpen
ImportFile = .FileName
End With
If Len(ImportFile) > 0 Then
Debug.Print "filename=" & ImportFile
Text1.Text = ImportFile
DoEvents
'获取sheet名称
If InStr(ImportFile, "xlsx") > 0 Then
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ImportFile & _
";Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1'"
Else
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ImportFile & _
";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
End If
Set cat.ActiveConnection = cn
Combo1.Clear
For Each tbl In cat.Tables
Debug.Print tbl.Name
If InStr(tbl.Name, "$") > 0 Then
Combo1.AddItem tbl.Name
End If
Next
Combo1.Text = "请选择数据表名称"
Set tbl = Nothing
Set cat = Nothing
Set cn = Nothing
End If
MyError:
If Err.Number = 0 Then Exit Sub
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "错误"
End Sub
2016年06月26日 13点06分 5
后期绑定方式,以兼容性换性能,不需要引用Excel组件,适用于所有Excel版本,如果电脑装是WPS的话,还需要加et跟ket创建对象测试
2017年08月28日 08点08分
level 7
暴牙归来 楼主
@闪星2 我根据你的代码做了相关修改,但结果是这样的
修改后的代码:
Try
Dim cat As Object
Dim cn As Object
Dim tbl As Object
Dim xlssql As String
cn = CreateObject("provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & Me.ListBox1.SelectedItems(n))
cat = CreateObject(Me.ListBox1.SelectedItems(n))
Debug.Print("filename=" & Me.ListBox1.SelectedItems(n))
If InStr(Me.ListBox1.SelectedItems(n), "xlsx") > 0 Then
cn.Open("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & Me.ListBox1.SelectedItems(n) & ";Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1'")
Else
cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Me.ListBox1.SelectedItems(n) & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'")
End If
cat.ActiveConnection = cn
For Each tbl In cat.Tables
Debug.Print(tbl.Name)
If InStr(tbl.Name, "$") > 0 Then
Me.ListBox2.Items.Add(tbl.Name)
End If
Next
tbl = Nothing
cat = Nothing
cn = Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "工作表读取失败")
End Try
其中的Me.ListBox1.SelectedItems(n)是指下面这个
2016年06月27日 07点06分 6
顺便问一下ActiveX组件在哪里下载比较放心@闪星2
2016年06月27日 07点06分
level 12
调用Microsoft Excel XX Object Library组件就可以了
Imports Microsoft.Office.Interop
Private Sub testSub()
Dim xlApp = New Excel.Application
Dim xlBook = xlApp.Workbooks.Open("Excel文件路径")
For Each sheet As Excel.Worksheet In xlBook.Sheets
MsgBox(sheet.Name)
Next
End Sub
2016年06月27日 13点06分 7
用完对象以后,记得收拾 xlBook.Close(False) '不保存文件 xlApp.Quit()
2016年06月27日 13点06分
不行啊,Dim xlApp = New Excel.Application 等号后面画波浪线
2016年06月27日 14点06分
@暴牙归来 第一,要调用excel的com组件,第二,要用imports把office的名字空间导进来
2016年06月27日 14点06分
@lzb148 com组件有excel开头的控件吗
2016年06月28日 04点06分
level 11
cn = CreateObject("ADODB.Connection")
cat = CreateObject("ADOX.Catalog")
这里酱紫改就可以了呀
2016年06月27日 16点06分 9
"ADODB.Connection"不就是连接字符串吗?"ADOX.Catalog"不就是数据库吗
2016年06月28日 04点06分
难道你要叫我直接照抄??@闪星2
2016年06月28日 04点06分
@暴牙归来 没抄呀,不是去掉了set吗? 这两句就是去掉set就可以了
2016年06月28日 10点06分
level 1
‘获取当前 excel 文件名:
Private Sub command1_click()
Dim xl As Object ' New Excel.Application '
Dim xlBook As Object ' Workbook
Set xl = GetObject(, "Excel.Application")
Set xlBook = xl.ActiveWorkbook
Debug.Print xlBook.Path & "\" & xlBook.name '立即窗口显示
msgbox xlBook.Path & "\" & xlBook.Name '弹窗显示
end sub
2017年08月18日 07点08分 10
level 13
好东西
2017年08月27日 21点08分 11
1