"对象引用未设置为对象的实例"使用复选框清除数据表时

时间:2018-02-04 15:02:51

标签: excel vb.net

我有一个带有绑定DGV的表。该表有12列,其中一列是复选框。该表中包含数据,没有任何问题。

我想清除表格,然后从Excel将数据导入表格。 问题是我清除表并发生异常。这是因为复选框具有空值。

其他表没有问题,没有复选框。我可以清除表格并从Excel导入数据。

Try ... Catch程序甚至没有发现错误。

'清除DGV 尝试        DsAstroCatalogueData.tblMessierData.Clear()        赶上前例外          MessageBox.Show(ex.Message,Me.Text,MessageBoxButtons.OK,MessageBoxIcon.Error)
最后
  '没有 结束尝试

完整代码:

Private Sub MnuImportFromExcel_Click(sender As Object, e As EventArgs) Handles mnuImportFromExcel.Click

        Dim strExcelFilePath As String = ""

        Dim strCellValue As String = ""

        Dim strColumn1 As String = ""
        Dim strColumn2 As String = ""
        Dim strColumn3 As String = ""
        Dim strColumn4 As String = ""
        Dim strColumn5 As String = ""
        Dim strColumn6 As String = ""
        Dim strColumn7 As String = ""
        Dim strColumn8 As String = ""
        Dim strColumn9 As String = ""
        Dim strColumn10 As String = ""
        Dim boolColumn11 As Boolean = False
        Dim strColumn12 As String = ""

        Dim intColumnIndex As Integer = 0
        Dim intRowIndex As Integer = 0

        Dim intItemsAddedCount As Integer = 0

        statusCatalogProgressBar.Value = 0

        Try
            'Select Excel File
            With OpenFileDialogExcel()
                .Filter = "Excel Files|*.xls;*.xlsx"
                .Title = "Select- Excel Configuration File"
                .FileName = ""
            End With

            If (OpenFileDialogExcel.ShowDialog() = DialogResult.OK) Then
                strExcelFilePath = OpenFileDialogExcel.FileName
            Else
                strExcelFilePath = ""
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            'Nothing
        End Try


        If strExcelFilePath IsNot "" Then

            'Update Status Bar Text
            statusCatalogFrm.Text = "Importing Catalog from Excel..."
            statusCatalogProgressBar.Visible = True
            statusCatalogRightLabel.Visible = False

            Dim xlAppEyepieces As New Excel.Application
            Dim xlWorkBookEyepieces As Excel.Workbook
            Dim xlWorkSheetEyepieces As Excel.Worksheet
            Dim xlRangeEyepieces As Excel.Range


            Try
                ' Open Excel Spreadsheet.
                xlWorkBookEyepieces = xlAppEyepieces.Workbooks.Open(strExcelFilePath)

                ' Loop over all sheets.
                For i As Integer = 1 To xlWorkBookEyepieces.Sheets.Count

                    ' Get Worsheet Data.
                    xlWorkSheetEyepieces = CType(xlWorkBookEyepieces.Sheets(i), Worksheet)


                    ' Get Data Range.
                    xlRangeEyepieces = xlWorkSheetEyepieces.UsedRange

                    ' Load all cells into 2d array.
                    Dim objExcelArray(,) As Object = CType(xlRangeEyepieces.Value(XlRangeValueDataType.xlRangeValueDefault), Object(,))

                    'Close COM Objects
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRangeEyepieces)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheetEyepieces)
                    ReleaseObject(xlRangeEyepieces)
                    ReleaseObject(xlWorkSheetEyepieces)

                    ' Scan the cells.
                    'If objExcelArray IsNot Nothing Then

                    ' Get bounds of the array.
                    Dim NumberOfRows As Integer = objExcelArray.GetUpperBound(0)  'Rows
                    Dim NumberOfColumns As Integer = objExcelArray.GetUpperBound(1)  'Columns

                    'Progress Bar Maximum Value
                    statusCatalogProgressBar.Maximum = NumberOfRows

                    'Clear DGV
                    Try
                        DsAstroCatalogueData.tblMessierData.Clear()
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Finally
                        'Nothing
                    End Try

                    ' Loop over all elements.
                    For intRowIndex = 0 To NumberOfRows - 2

                            For intColumnIndex = 1 To NumberOfColumns

                                Try
                                    'Test for NULL Value
                                    If objExcelArray(intRowIndex + 2, intColumnIndex) IsNot Nothing Then

                                        strCellValue = objExcelArray(intRowIndex + 2, intColumnIndex).ToString

                                        Select Case intColumnIndex
                                            Case 1
                                                strColumn1 = strCellValue
                                            Case 2
                                                strColumn2 = strCellValue
                                            Case 3
                                                strColumn3 = strCellValue
                                            Case 4
                                                strColumn4 = strCellValue
                                            Case 5
                                                strColumn5 = strCellValue
                                            Case 6
                                                strColumn6 = strCellValue
                                            Case 7
                                                strColumn7 = strCellValue
                                            Case 8
                                                strColumn8 = strCellValue
                                            Case 9
                                                strColumn9 = strCellValue
                                            Case 10
                                                strColumn10 = strCellValue
                                            Case 11
                                                boolColumn11 = False
                                            Case 12
                                                strColumn12 = strCellValue
                                        End Select

                                    Else
                                        Select Case intColumnIndex
                                            Case 1
                                                strColumn1 = ""
                                            Case 2
                                                strColumn2 = ""
                                            Case 3
                                                strColumn3 = ""
                                            Case 4
                                                strColumn4 = ""
                                            Case 5
                                                strColumn5 = ""
                                            Case 6
                                                strColumn6 = ""
                                            Case 7
                                                strColumn7 = ""
                                            Case 8
                                                strColumn8 = ""
                                            Case 9
                                                strColumn9 = ""
                                            Case 10
                                                strColumn10 = ""
                                            Case 11
                                                boolColumn11 = False
                                            Case 12
                                                strColumn1 = ""
                                        End Select

                                    End If

                                Catch ex As Exception
                                    MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                                Finally
                                    'Nothing
                                End Try

                            Next

                            'Add data to Data Table
                            'If (strColumn1 IsNot "") And (strColumn2) IsNot "" Then
                            With DsAstroCatalogueData
                                .Tables("tblMessierData").Rows.Add()
                                .Tables("tblMessierData").Rows(intRowIndex).Item("NGC") = strColumn1
                                .Tables("tblMessierData").Rows(intRowIndex).Item("Messier") = strColumn2
                                .Tables("tblMessierData").Rows(intRowIndex).Item("ObjectType") = strColumn3
                                .Tables("tblMessierData").Rows(intRowIndex).Item("ObjectName") = strColumn4
                                .Tables("tblMessierData").Rows(intRowIndex).Item("Constellation") = strColumn5
                                .Tables("tblMessierData").Rows(intRowIndex).Item("Magnitude") = strColumn6
                                .Tables("tblMessierData").Rows(intRowIndex).Item("RightAscension") = strColumn7
                                .Tables("tblMessierData").Rows(intRowIndex).Item("Declination") = strColumn8
                                .Tables("tblMessierData").Rows(intRowIndex).Item("ObjectSize") = strColumn9
                                .Tables("tblMessierData").Rows(intRowIndex).Item("Comment") = strColumn10
                                .Tables("tblMessierData").Rows(intRowIndex).Item("Viewed") = boolColumn11
                                .Tables("tblMessierData").Rows(intRowIndex).Item("ImageFile") = strColumn12
                            End With

                        intItemsAddedCount = (intRowIndex + 1)

                        'Update Staus Bar
                        statusCatalogFrm.Text = "Importing Catalog from Excel... [" & intItemsAddedCount & " of " & (NumberOfRows - 1) & " rows]"
                        statusCatalogProgressBar.Value = intItemsAddedCount
                        StatusStripMessierFrm.Update()

                        'End If

                        strCellValue = ""

                        Next


                    'End If
                Next

                dgvMessierData.Refresh()

                'Data Import Completed
                MessageBox.Show("Data Import Complete" & vbLf & vbLf & intItemsAddedCount &
                            " items added", "AstroCompanion - Import Catalog Data from Exel",
                               MessageBoxButtons.OK, MessageBoxIcon.Information)

                'Update Status Bar Text
                statusCatalogFrm.Text = "Ready"
                statusCatalogProgressBar.Value = 0
                statusCatalogProgressBar.Visible = False



                'Close all Excel items
                xlAppEyepieces.Workbooks.Close()
                xlAppEyepieces.Quit()
                GC.Collect()
                GC.WaitForPendingFinalizers()

                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBookEyepieces)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlAppEyepieces)

                ReleaseObject(xlWorkBookEyepieces)
                ReleaseObject(xlAppEyepieces)


            Catch ex As Exception
                MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                GC.Collect()
            End Try


        End If

    End Sub

0 个答案:

没有答案