我写了将datagridview导出到excel的代码,错误是未声明发布对象

时间:2019-06-18 11:17:19

标签: vb.net datagridview export-to-excel

错误是 releaseobject未声明 ,我做了所有参考 但是仍然会发生错误。

尝试         Button5.Text =“请稍候...”         Button5.Enabled = False

    SaveFileDialog1.Filter = "Excel Document (*.xlsx)|*.xlsx"
    If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Microsoft.Office.Interop.Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For i = 0 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                For k As Integer = 1 To DataGridView1.Columns.Count
                    xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                    xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
                Next
            Next
        Next

        xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp) '<-here is error
        releaseObject(xlWorkBook)'<-here is error
        releaseObject(xlWorkSheet)'<-here is error

        MsgBox("Successfully saved" & vbCrLf & "File are saved at : " & SaveFileDialog1.FileName, MsgBoxStyle.Information, "Information")

        Button5.Text = "Export To MS Excel"
        Button5.Enabled = True
    End If
Catch ex As Exception
    MessageBox.Show("Failed to save !!!", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Return
End Try

这是图像的链接 https://cloud.mail.ru/public/2MVJ/4ATQCbjsg

0 个答案:

没有答案