I want a help on the title problem ... I know I can do this import through VBA, but I want my program to detect the .xls format of a spreadsheet and extract its data to a ListBox within Visual Studio.
What is it like?
I want a help on the title problem ... I know I can do this import through VBA, but I want my program to detect the .xls format of a spreadsheet and extract its data to a ListBox within Visual Studio.
What is it like?
If you are not afraid to work "in the dark", that is without help from intellisense and Visual Studio error detection, you can reference the Excel application as Object and use the same methods which I would use in VBA. This is called Late Binding .
The code below, for example, tries to appropriate an instance of Excel that is already running (with GetObject () ). If not, the error is handled by creating a new instance (with CreateObject () ).
From there, we are in familiar terrain. The workbook located in "C: \ Users \ MyOffers \ Desktop \ Spreadsheet.xls" opens and the cells in the "A1: A10" range of the "Plan1" worksheet are scanned. The values are copied to a List (Of String) where they will be available for you to use as you wish:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Excel As Object
Try
Excel = GetObject(, "Excel.Application")
Catch ex As Exception
Excel = CreateObject("Excel.Application")
End Try
Excel.Visible = True
Dim WorkBook As Object = Excel.Workbooks.Open("C:\Users\Fulano\Desktop\Planilha.xls")
Dim WorkSheet As Object = WorkBook.Worksheets("Plan1")
Dim MyList As New List(Of String)
Dim Range = WorkSheet.Range("A1:A10")
For Each Cell In Range.Cells
MyList.Add(Cell.Value)
Next
WorkBook.Close
Excel.Quit
Excel = Nothing
Stop
End Sub
I hope this helps.