问题描述
我将数据从excel文档插入vb.net中的datagridview控件,以下代码工作正常,只有excel中的工作表名为Sheet1。 br />
I am inserting data from excel document to datagridview control in vb.net the following code works fine, only if the sheet in the excel is named Sheet1.
Dim conn As OleDbConnection
Dim dtr As OleDbDataReader
Dim dta As OleDbDataAdapter
Dim cmd As OleDbCommand
Dim dts As DataSet
Dim excel As String
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim fi As New FileInfo(OpenFileDialog.FileName)
Dim FileName As String = OpenFileDialog.FileName
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
dta = New OleDbDataAdapter("Select * From [Sheet1$]", conn)
dts = New DataSet
dta.Fill(dts, "[Sheet1$]")
DataGridView1.DataSource = dts
DataGridView1.DataMember = "[Sheet1$]"
conn.Close()
现在的问题是工作表可能有不同的名称,我想获得excel文档中的第一个工作表,不管它是什么名称。任何有关这方面的帮助将非常感谢。
谢谢
The problem now is the sheet might have a different name and i want to get the first sheet in the excel document despite what name it is. Any help regarding this will be highly appreciated.
Thanks
推荐答案
现在的问题是工作表可能有不同的名称,我想获得excel文档中的第一张表,不管它是什么名字。任何帮助关于这一点将非常感谢。
谢谢
The problem now is the sheet might have a different name and i want to get the first sheet in the excel document despite what name it is. Any help regarding this will be highly appreciated.
Thanks
这篇关于将数据从excel导入到vb.net中的datagrid视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!