如何从Excel文件中获取列名称 [英] How Can I Get The Column Names From The Excel File
本文介绍了如何从Excel文件中获取列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用ofd作为新的OpenFileDialog
如果ofd.ShowDialog = Windows.Forms.DialogResult.OK那么
txtExcelPath.Text = ofd.FileName
结束如果
结束使用
我从上面编码得到了文件路径。我不知道如何从文件中检索列名。请帮帮我。
先谢谢
Using ofd As New OpenFileDialog
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtExcelPath.Text = ofd.FileName
End If
End Using
I got the file path from above coding.i don't know how to retrieve the column names from the file.Please help me.
Thanks in Advance
推荐答案
1。您可以通过ADODB连接与excel表连接
1. You can connect by ADODB connection with excel sheet
Dim cn as ADODB.Connection
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=C:\MyFolder\MyWorkbook.xls;" & _
"Extended Properties=Excel 8.0;"
.Open
End With
2.然后将所有数据存入数据集,然后您可以通过以下行获取列名
2. Then get all data to a dataset and then you can get column name by following line
dt.Columns[0].ColumnName
参见使用MS Excel(xls / xlsx)使用MDAC和Oledb [ ^ ],其中包含VB.NET示例代码。
See Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^], which includes VB.NET sample code.
这篇关于如何从Excel文件中获取列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文