: : : : : As a beginner to VB.NET I need help retrieving data from an EXCEL spreadsheet into a VB Listbox. can anyone help with this problem?
: : : : :
: : : : : Exactly what are yout trying to do? First, you need to add a reference to the excel object library. After that, you can access the excel object model. But here's a lil code to help you get started:
: : : :
: : : : Dim oExcel As Excel.Application
: : : : Dim oDoc As Excel.Workbook
: : : : Dim oWorksheet As Excel.Worksheet
: : : :
: : : : oExcel = New Excel.Application
: : : : oExcel.Visible = False
: : : : 'oExcel.WindowState = XlWindowState.xlMinimized
: : : :
: : : : oDoc = oExcel.Workbooks.Open(FileName) 'FileName is the full path and
: : : : 'name of the excel document.Ex:
: : : : '"C:\File.xls"
: : : : For i = 1 To oDoc.Worksheets.Count
: : : : oWorksheet = oDoc.Worksheets.Item(i) 'Loop through each worksheet
: : : : 'Now that you have a reference to the worksheet,
: : : : 'you can access the properties and methods
: : : : 'of a worksheet to retrieve the data that is needed.
: : : : Next
: : : :
: : : : Hope this helps>>>
: : : :
: : : Why do I get this message
: : : Type Excel.Aplication not defined
: : : I've added the Excel 11.0 object Library from the Add Reference box
: : : is there still something missing?
: : :
: : : did you type application right? or are you using it outside the sub, function, or module that it was declare in?
: :
: :
: This is a Cut and Paste of all the code, I cannot see anything out of the ordinary do you. I am using Visual Studio 2005 with the MSDN Object Libraries installed.
:
: Public Class Form1
:
: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
: Dim oExcel As Excel.Application
: Dim oDoc As Excel.Workbook
: Dim oWorksheet As Excel.Worksheet
:
: oExcel = New Excel.Application
: oExcel.Visible = False
: 'oExcel.WindowState = XlWindowState.xlMinimized
:
: oDoc = oExcel.Workbooks.Open("C:\bhandicap060926.xls") 'FileName is the full path and
: 'name of the excel document.Ex:
: '"C:\File.xls"
: 'For i = 1 To oDoc.Worksheets.Count
: 'oWorksheet = oDoc.Worksheets.Item(i) 'Loop through each worksheet
:
: End Sub
: End Class
:
: And this is the frist of 4 errors I get.
:
:
: Error 1 Type 'Excel.Application' is not defined. C:\Documents and Settings\Randyf\My Documents\Visual Studio 2005\Projects\oExcel\oExcel\Form1.vb 4 23 oExcel
:
: This could be it. Did you include the imports statement. The imports statement is usually the first line in any class or module that might using the references: This statement should be on the very top of the code....................
Imports Microsoft.Office.Interop.Excel or Imports Excel
:
: