WPF:如何在vb.net中检索excel工作表名称而不打开excel文件?
xamlWPF:如何在vb.net中检索excel工作表名称而不打开excel文件?,wpf,vb.net,ole,Wpf,Vb.net,Ole,xaml 当我运行上面的代码时,我得到了这个错误:下面的代码可能对您有帮助。此处fname称为excel文件位置 Imports System.Data Class MainWindow Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click Dim myOleDbConnection As New System.Data.OleDb.OleDbConnection("
当我运行上面的代码时,我得到了这个错误:下面的代码可能对您有帮助。此处fname称为excel文件位置
Imports System.Data
Class MainWindow
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
Dim myOleDbConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Book1.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")
myOleDbConnection.Open()
Dim myDataTable As System.Data.DataTable = myOleDbConnection.GetOleDbSchemaTable(schema:=System.Data.OleDb.OleDbSchemaGuid.Tables, restrictions:=Nothing)
Combobox1.ItemsSource = (myOleDbConnection.GetSchema("Tables", New String() {Nothing, Nothing, Nothing, "TABLE"}).AsEnumerable().Select(Function(d) d("TABLE_NAME").ToString.Replace("$", "")).Distinct().ToArray)
myOleDbConnection.Close()
End Sub
End Class
实际上,location.text是一个标签,我用组合框绑定,所以删除了这些行。实际代码如下:Conn.GetSchema(“Tables”,New String(){Nothing,Nothing,Nothing,“TABLE”})。AsEnumerable().Select(函数(d)d(“TABLE_NAME”).ToString.Replace(“$”,”).Distinct().ToArray)我正在使用LINQ QUERY在不打开文件的情况下,无法判断文件中的内容。
Imports System.Data
Class MainWindow
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
Dim myOleDbConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Book1.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";")
myOleDbConnection.Open()
Dim myDataTable As System.Data.DataTable = myOleDbConnection.GetOleDbSchemaTable(schema:=System.Data.OleDb.OleDbSchemaGuid.Tables, restrictions:=Nothing)
Combobox1.ItemsSource = (myOleDbConnection.GetSchema("Tables", New String() {Nothing, Nothing, Nothing, "TABLE"}).AsEnumerable().Select(Function(d) d("TABLE_NAME").ToString.Replace("$", "")).Distinct().ToArray)
myOleDbConnection.Close()
End Sub
End Class
Try
Dim StrConn As [String] = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fname & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
Dim Conn As New OleDbConnection(StrConn)
Conn.Open()
'Dim dtSheets As DataTable =
' Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "Table"})
'Dim listSheet As New List(Of String)
Location.Text = fname 'Label Control
SheetName.Items.Clear() ' Combobox clear
SheetName.Items.AddRange(Conn.GetSchema("Tables", New String() {Nothing, Nothing, Nothing, "TABLE"}
).AsEnumerable().Select(Function(d) d("TABLE_NAME").ToString.Replace("$", "")).Distinct().ToArray) 'Binding All sheetname to combobox
Catch ex As Exception
Log.ErrorMessage(ex.Message, Me.Name) 'local log creation manual code
End Try