Question Export Datagridview to Microsoft Excel 2013

Benniit

Member
Joined
Mar 14, 2009
Messages
12
Programming Experience
Beginner
Please I'm using vb.net 2013 and Microsoft office 2013. The below code is able to display the datagridview
into excel 2007 using vb.net 2008. But the code does not display onto excel 2013 and no error gets reported. I suspect the
excel 2013 format could be the cause but i do not know how to go about it. Please help me out. If you could please link me to any code that can display Datagridview into excel 2013. Thanks, Ben


'Assuming DatagridView has already been loaded.
VB.NET:
 Dim ExcelApp As Object, ExcelBook As Object
        Dim ExcelSheet As Object
        Dim i As Integer
        Dim j As Integer
 
        'create object of excel
        ExcelApp = CreateObject("Excel.Application")
        ExcelBook = ExcelApp.WorkBooks.Add
        ExcelSheet = ExcelBook.WorkSheets(1)
 
        With ExcelSheet
            For i = 1 To Me.DataGridViewGCL.RowCount
                .cells(i, 1) = Me.DataGridViewGCL.Rows(i - 1).Cells("GclNo").Value
                For j = 1 To DataGridViewGCL.Columns.Count - 1
                    .cells(i, j + 1) = DataGridViewGCL.Rows(i - 1).Cells(j).Value
                Next
            Next
        End With
 
        ExcelApp.Visible = True
        '
        ExcelSheet = Nothing
        ExcelBook = Nothing
        ExcelApp = Nothing
 
How exactly have you referenced the Excel object model? I don't really use Office so I don't know all the details but it was the case that if you referenced a specific version of the Excel object library then your code would only work with that version. I believe that there is now a way to be able to support all versions without having to resort to late binding but if you're not using that method then your code will not work with any version of Excel other than the one you've referenced.
 

Latest posts

Back
Top