1.获取第一个sheet的名称。
Public Shared Function GetFirstSheetName(ByVal strFilePath As String) As String
Dim myExcel As Excel.Application = New Excel.Application
Dim missing As Object = System.Reflection.Missing.Value
myExcel.Application.Workbooks.Open(strFilePath)
Dim mybook As Excel.Workbook = myExcel.Workbooks(1)
Dim sheet As Excel.Worksheet = mybook.Sheets(1)
Dim strSheetName As String = sheet.Name
mybook.Close()
myExcel.Quit()
Return strSheetName
End Function
2.获取全部sheet的名称。
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