Zasadniczo kopia Anirudh Gau r odpowiedź. Zrobiłem formatowanie kodu i wstawiłem go do funkcji. Dodałem StringBuilder, dzięki czemu mogę zrobić więcej za pomocą instrukcji SELECT.
Upvotes iść do Anirudh Gaur
Private Function Load_XLS(FileName As String) As DataTable
Dim DataTable As New DataTable
Dim Format As String = ""
If IO.Path.GetExtension(FileName) = ".xls" Then
Format = "Excel 8.0"
ElseIf IO.Path.GetExtension(FileName) = ".xlsx" Then
Format = "Excel 12.0"
End If
Using Connection As New OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & FileName & "';Extended Properties=" & Format & ";")
Connection.Open()
Dim TableName As String = Connection.GetSchema("Tables").Rows(0)("TABLE_NAME")
Dim SQLCommand As New Text.StringBuilder
SQLCommand.AppendLine("SELECT *")
SQLCommand.AppendLine("FROM [{0}]")
Dim Command As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(String.Format(SQLCommand.ToString, TableName), Connection)
Command.Fill(DataTable)
Connection.Close()
End Using
Return DataTable
End Function
możliwe duplikat [Using Excel OleDb uzyskać nazwy arkuszy w arkuszu ORDER] (http://stackoverflow.com/questions/1164698/using-excel-oledb-to -get-name-in-sheet-order) –