如何检查excel文件中是否存在具有给定名称的列

时间:2014-06-10 23:34:44

标签: vb.net

我有一个包含数据列的excel文件,为了让我能够使用excel文件,如果存在某些列,我首先要知道。我正在使用Microsoft Visual Studio我一直在做一些研究并找到了这个。

 If DataRow.Table.Columns.Contains("column") Then
    MsgBox("YAY")
End If

这个

 colEmp = Rows(1).Find("Emp#", LookAt:=xlWhole).Column
 colEmpName = Rows(1).Find("EmpName", LookAt:=xlWhole).Column
 colDepartment = Rows(1).Find("Department", LookAt:=xlWhole).Column
 colOrganization = Rows(1).Find("Organization", LookAt:=xlWhole).Column

不确定如何在按钮中应用它。谢谢

1 个答案:

答案 0 :(得分:1)

向表单和事件处理程序添加一个按钮:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    colEmp = Rows(1).Find("Emp#", LookAt:=xlWhole).Column
    colEmpName = Rows(1).Find("EmpName", LookAt:=xlWhole).Column
    colDepartment = Rows(1).Find("Department", LookAt:=xlWhole).Column
    colOrganization = Rows(1).Find("Organization", LookAt:=xlWhole).Column

    'Add whatever else you want to do here...

End Sub