Imports Microsoft.VisualBasic
Public Class Class1
Public Shared Function GetAllSheetName(ByVal strFilePath As String) As String()
Dim strConn As String = String.Empty
If strFilePath.EndsWith("xls") Then
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; " + _
"Data Source=" + strFilePath + "; " + _
"Extended Properties='Excel 8.0;IMEX=1'"
ElseIf strFilePath.EndsWith("xlsx") Then
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + _
"Data Source=" + strFilePath + ";" + _
"Extended Properties='Excel 12.0;HDR=YES'"
End If
Dim conn As OleDbConnection = New OleDbConnection(strConn)
conn.Open()
Dim sheetNames(conn.GetSchema("Tables").Rows.Count - 1) As String
For i As Integer = 0 To conn.GetSchema("Tables").Rows.Count - 1
sheetNames(i) = conn.GetSchema("Tables").Rows(i)("TABLE_NAME").ToString
Next
conn.Close()
Return sheetNames
End Function
End Class
本文介绍了一种使用Visual Basic编程语言从Excel文件(包括.xls和.xlsx格式)中获取所有工作表名称的方法。通过建立OLE DB连接并利用GetSchema方法,可以有效地检索到Excel文件内的所有工作表名。
820

被折叠的 条评论
为什么被折叠?



