Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, I want to get the sheet names of excel or any other file using vb.net . I wrote

code here is

VB
Dim excel As String = frmImportFormBSurveyedData.txtFilePath.Text

      Dim xl As New Excel.Application

      Dim xlsheet As Excel.Worksheet
      Dim xlwbook As Excel.Workbook

      xlwbook = xl.Workbooks.Open(excel)
      xlsheet = xlwbook.Sheets.Item(1)

      For Each sht In xlwbook.Worksheets
          lsbSheetName.Items.Add(sht.Name)


      Next

      xl.ActiveWorkbook.Close(False, Excel)
      xl.Quit()

      xlwbook = Nothing
      xl = Nothing




It's working file but any other way to get the sheet names using vb.net

please anyone help me.

advanced thanks....
Posted

Reading and Writing Excel using OLEDB[^] explains how to do it. The article is C# but it should be easy to translate to VB.NET.
 
Share this answer
 
tyr This code in vb.net

<pre lang="vb">Private Sub GetExcelSheetNames(ByVal fileName As String)
    Dim strconn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & 
          fileName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    Dim conn As New OleDbConnection(strconn)

    conn.Open()

    Dim dtSheets As DataTable = 
              conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    Dim listSheet As New List(Of String)
    Dim drSheet As DataRow

    For Each drSheet In dtSheets.Rows
        listSheet.Add(drSheet("TABLE_NAME").ToString())
    Next

    //show sheetname in textbox where multiline is true
    For Each sheet As String In listSheet
        TextBox1.Text = TextBox1.Text & sheet & vbNewLine
    Next

    conn.Close()

End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900