vb.net取得Excel中的全部sheet名称(亲自实践)

前端之家收集整理的这篇文章主要介绍了vb.net取得Excel中的全部sheet名称(亲自实践)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

猜你在找的VB相关文章