vb.net – 需要将DataGridView导出到excel文件中

我的程序包含一个datagridview,用户在运行时手动填充数据.当用户单击“保存”时,我希望将datagridview导出到以后可以访问的excel文件中.

我现在的代码出现了一个错误类型“Null Reference Exception”

Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()

    If xlApp Is Nothing Then
        MessageBox.Show("Excel is not properly installed!!")
        Return
    End If


    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer

    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")
    'xlWorkSheet.Cells(1, 1) = "Sheet 1 content"
    'xlWorkSheet.Cells(3, 3) = "Sheet 1 content"

    If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
        Exit Sub
    End If



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

            xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()

        Next
    Next



    xlWorkBook.SaveAs("C:\test\DATAGRID.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
     Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
    xlWorkBook.Close(True, misValue, misValue)
    xlApp.Quit()

    releaseObject(xlWorkSheet)
    releaseObject(xlWorkBook)
    releaseObject(xlApp)

    MessageBox.Show("Excel file created , you can find the file C:\test\DATAGRID.xls")

最佳答案 谢谢大家的帮助

我发现这段代码实际上做了我想要的

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim rowsTotal, colsTotal As Short
    Dim I, j, iC As Short
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
    Dim xlApp As New Excel.Application
    Dim misValue As Object = System.Reflection.Missing.Value


    Try
        Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
        Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)

        rowsTotal = DataGridView1.RowCount - 1
        colsTotal = DataGridView1.Columns.Count - 1
        With excelWorksheet
            .Cells.Select()
            .Cells.Delete()
            For iC = 0 To colsTotal
                .Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
            Next
            For I = 0 To rowsTotal - 1
                For j = 0 To colsTotal - 1
                    .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
                Next j
            Next I
            .Rows("1:1").Font.FontStyle = "Bold"
            .Rows("1:1").Font.Size = 10
            .Cells.Columns.AutoFit()
            .Cells.Select()
            .Cells.EntireColumn.AutoFit()
            .Cells(1, 1).Select()
        End With
        excelBook.SaveAs("C:\test\DATAGRID.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
   Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
        MessageBox.Show("Excel file created , you can find the file C:\test\DATAGRID.xls")




    Catch ex As Exception
        MsgBox("Export Excel Error " & ex.Message)
    Finally
        'RELEASE ALLOACTED RESOURCES
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
        xlApp = Nothing
    End Try
End Sub
点赞