Check if sheet exists in Excel ?

  • Thread starter Thread starter Shan1986
  • Start date Start date
S

Shan1986

Guest
Hi, I would like check if particular sheets exist in an Excel file, I am using following code to read sheets to datatable but i want check if the sheets exists before. How can i do that?
Try

Dim MyConnection As OleDbConnection
Dim MyCommand As OleDbDataAdapter
Dim path As String = "C:\temp\Sample.xlsx"
Dim sheetz As Array = {"Sheet1", "Sheet2", "Sheet3"}

MyConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")

For i = LBound(sheetz) To UBound(sheetz)

MyCommand = New OleDbDataAdapter("select * from [" & sheetz(i) & "$]", MyConnection)
Dim dtImport As DataTable = New DataTable()
MyCommand.Fill(dtImport)
MTO_DS.Tables.Add(dtImport)

Next

DataGridView1.DataSource = MTO_DS.Tables(0)
DataGridView2.DataSource = GetTable() 'MTO_DS.Tables(1)

MyConnection.Close()

MTO_DS.Tables(0).Merge(MTO_DS.Tables(5))

Catch ex As Exception
MsgBox(ex.Message.ToString)

End Try

Continue reading...
 
Back
Top