Selecting a Worksheet name after browse to file location

Moordoom

Member
Joined
Nov 12, 2013
Messages
23
Programming Experience
1-3
I have Browse button code, that when I browse to an Excel file, I then want to be able to select a particular worksheet for that file manually so I can then import that particular sheet into a Datagridview. I am trying to use either a combobox or a listbox.
My plans are to use the Browse button to populate a Textbox, then via that textbox, select a worksheet with the combobox or listbox, then import it to a Datagridview for review, before exporting it to a SQL Database table.
I have to code for the browse button, I can import the file to a dataviewgrid,and export it to SQL. But only if the Excel file has a Single "predefined" worksheet name. I need the worksheet selectable after the browse but before the Datagridview import. This is my Browse Button Code.I am guessing its to simplistic for the takes and must load the file first.
VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim fd As OpenFileDialog = New OpenFileDialog()
    Dim strFileName As String

    fd.Title = "Open File Dialog"
    fd.InitialDirectory = "H:\COST ACCTG\Inventory\"
    fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
    fd.FilterIndex = 2
    fd.RestoreDirectory = True

    If fd.ShowDialog() = DialogResult.OK Then
        strFileName = fd.FileName
    End If

    TextBox1.Text = strFileName

End Sub
 
How are you reading the data? Are you using ADO.NET or Office Automation? There's a possibility that ADO.NET can be used to read the sheet list too but I'm not sure, while Automation definitely can.
 
I haven't started reading the data. I plan to use ADO.Net. The file is not loaded yet. The browse code is just to select the file and put the location in the text box.
I just did not know if there was a way to get that information before loading the Excel file to memory.
 
If you're using ADO.NET then you'll presumably be using an OleDbConnection and the Jet or ACE OLE DB provider. In that case, your connection object has a GetSchema method that can be used to get various schema information, including table names. That works for proper databases like Access so it may also work for Excel files, with each worksheet being considered a table. I'm not sure though, as I've never tried with a data source that isn't a proper database.
 

Latest posts

Back
Top