none
export datagridview to excel 2007 (vb2010) RRS feed

  • Question

  • hi guys,

    here's my code (in bold the line were i get the error). Error message is: HRESULT: 0x8002000B (DISP_E_BADINDEX) COMException.

    Can anyone help me please??!!

    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 Dgvdektops.RowCount - 2
                For j = 0 To Dgvdektops.ColumnCount - 1
                    For k As Integer = 1 To Dgvdektops.Columns.Count
                        xlWorkSheet.Cells(1, k) = Dgvdektops.Columns(k - 1).HeaderText
                        xlWorkSheet.Cells(i + 2, j + 1) = Dgvdektops.Item(j, i).Value.ToString()
                    Next
                Next
            Next

            xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
            xlWorkBook.Close()
            xlApp.Quit()

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

            MsgBox("You can find the file C:\vbexcel.xlsx")
        End Sub

        Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub

    End Class

    Tuesday, July 19, 2011 12:45 PM

Answers

  • Already solved it!

     

    Sheet1 needs to be in Dutch because i have a dutch version of course... Took me long enough to figure tis out..

     

    Sorry to have wasted your time. Tough thanks for helping.

     

    greetings

    • Marked as answer by hans_1983 Tuesday, July 19, 2011 1:49 PM
    Tuesday, July 19, 2011 1:49 PM

All replies

  • Hello,

    I'd try passing "Sheet1", not "sheet1". And just in case, make sure that worksheet exuists in the workbook.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, July 19, 2011 1:03 PM
  • Hi Andrei

     

    Thanks for answering.

    Changed sheet1 to Sheet1 with no succes. What do you mean by " just in case, make sure that worksheet exuists in the workbook"? Sorry i'm kinda new to visual basic

     

    thank you

    Grtz

    Tuesday, July 19, 2011 1:17 PM
  • Already solved it!

     

    Sheet1 needs to be in Dutch because i have a dutch version of course... Took me long enough to figure tis out..

     

    Sorry to have wasted your time. Tough thanks for helping.

     

    greetings

    • Marked as answer by hans_1983 Tuesday, July 19, 2011 1:49 PM
    Tuesday, July 19, 2011 1:49 PM