2017-04-05 67 views
1

即時通訊有一個錯誤,說:「對象引用未設置爲對象的實例。」我正在檢索excel文件並在vb.net的listview中導入,請幫助我是初學者。我有一個錯誤說「對象引用未設置爲對象的實例」。我是一個初學者

Imports MySql.Data.MySqlClient 

Public Class Form3 

    Public Myexcel As Microsoft.Office.Interop.Excel.Application 
    Dim completed As Boolean = False 
    Dim rows As New ExcelRows 
    Dim lvitem As ListViewItem 


    Private Structure ExcelRows 
     Dim id As String 
     Dim fname As String 
     Dim lname As String 
     Dim mi As String 
     Dim course As String 
     Dim Year As String 
     Dim no As String 
     Dim picture As String 
    End Structure 

    Private ExcelRowList As List(Of ExcelRows) = New List(Of ExcelRows) 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     OpenFileDialog1.FileName = Nothing 

     If OpenFileDialog1.ShowDialog = DialogResult.OK Then 
      TextBox1.Text = OpenFileDialog1.FileName 
     End If 

     If getinfo() = True Then 
      For Each xitem In ExcelRowList 
       lvitem = ListView1.Items.Add(xitem.no) 
       lvitem.SubItems.AddRange(New String() {xitem.id, xitem.lname, xitem.fname, xitem.course, xitem.Year, xitem.picture}) 


      Next 
     End If 


    End Sub 
    Private Function getinfo() As Boolean 

     Myexcel.Workbooks.Open(Me.TextBox1.Text) 

     Myexcel.sheet("EXPORT").Activate() 
     Myexcel.Range("A3").Activate() 

     Do 
      If Myexcel.ActiveCell.Value > Nothing Or Myexcel.ActiveCell.Text > Nothing Then 

       rows.no = Myexcel.ActiveCell.Value 
       Myexcel.ActiveCell.Offset(0, 1).Activate() 

       rows.id = Myexcel.ActiveCell.Value 
       Myexcel.ActiveCell.Offset(0, 1).Activate() 

       rows.lname = Myexcel.ActiveCell.Value 
       Myexcel.ActiveCell.Offset(0, 1).Activate() 

       rows.fname = Myexcel.ActiveCell.Value 
       Myexcel.ActiveCell.Offset(0, 1).Activate() 

       rows.mi = Myexcel.ActiveCell.Value 
       Myexcel.ActiveCell.Offset(0, 1).Activate() 

       rows.course = Myexcel.ActiveCell.Value 
       Myexcel.ActiveCell.Offset(0, 1).Activate() 

       rows.picture = Myexcel.ActiveCell.Value 

       ExcelRowList.Add(rows) 

       Myexcel.ActiveCell.Offset(1, -6).Activate() 

      Else 

       completed = True 
       Exit Do 

      End If 
     Loop 
     Myexcel.Workbooks.Close() 
     Myexcel = Nothing 


     Return completed 
    End Function 
End Class 
+0

使用調試器單步執行代碼以查找問題。目前,這個問題太模糊了。 – Ryan

回答

0

MyExcel對象似乎NUL因爲你沒有創造任何Microsoft.Office.Interop.Excel.Application實例。

嘗試:

Public Myexcel As New Microsoft.Office.Interop.Excel.Application() 

這應該解決你所得到的NUL參考例外。

相關問題