To read an excel file with Excel Class in vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nudrat
    New Member
    • Aug 2007
    • 38

    To read an excel file with Excel Class in vb.net

    I have added a reference Microsoft Excel 5.0 Object Library in my project.
    the code is as follows:
    Dim myExcel As Excel.Applicati on ' Interface to Excel
    Dim myWorkBookColle ction As Excel.Workbooks ' Workbook-collection
    Dim myWorkBook As Excel.Workbook ' Single Workbook (spreadsheet-collection)
    Dim myWorkSheet As Excel.Worksheet ' Single spreadsheet

    Dim fdlg As OpenFileDialog = New OpenFileDialog( )
    fdlg.Title = "VB.NET Corner Open File Dialog"
    'fdlg.InitialDi rectory = "c:\"
    fdlg.Filter = "Excel Files (*.Xls)|*.Xls"
    fdlg.FilterInde x = 2
    fdlg.RestoreDir ectory = True
    If fdlg.ShowDialog () = System.Windows. Forms.DialogRes ult.OK Then
    txtFile.Text = fdlg.FileName
    End If

    myExcel = GetObject(, "Excel.Applicat ion")
    If myExcel Is Nothing Then
    MessageBox.Show ("Could not load Excel.exe")
    Exit Sub
    End If
    ' initialise access to Excel's workbook collection
    myExcel.Visible = True
    myWorkBookColle ction = myExcel.Workboo ks
    'open spreadsheet from disk

    myWorkBook = myWorkBookColle ction.Open(txtF ile.Text, , False)
    'get 1st sheet from workbook
    myWorkSheet = myWorkBook.Shee ts.Item(1)
    'display the spreadsheet
    'myExcel.Visibl e = True 'Read Cell A2
    Dim myCell2 As Object = myWorkSheet.Ran ge("A2", System.Reflecti on.Missing.Valu e)
    Me.Text = myCell2.Value 'save and get out
    myWorkBook.Save ()
    myExcel.Quit()

    now its giving error at this line
    " myWorkBook = myWorkBookColle ction.Open(txtF ile.Text, , False)"

    it gives Member Not Found. whereas .open is there in the list.
    plz find the solution.
Working...