Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

Export DataGridView1 to Excel 15.0

HI

I have the below code that would tranfer my DataGridView1 into Excel.

Since i have Microsoft Office 15.0, the below code does not work.

Looks like this does not exist anymore:
       Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

How can i fix this.

Thanks again.

        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 Me.DataGridView1.RowCount - 1
            For j = 0 To Me.DataGridView1.ColumnCount - 1
                For k As Integer = 1 To Me.DataGridView1.Columns.Count
                    If Me.DataGridView1(j, i).Value IsNot Nothing Then
                        xlWorkSheet.Cells(1, k) = Me.DataGridView1.Columns(k - 1).HeaderText
                        xlWorkSheet.Cells(i + 2, j + 1) = Me.DataGridView1(j, i).Value.ToString()

                        xlWorkSheet.Cells(1, k).NumberFormat = "@"  '<<<<<                
                        xlWorkSheet.Cells(1, k) = Me.DataGridView1.Columns(k - 1).HeaderText
                        xlWorkSheet.Cells(i + 2, j + 1).NumberFormat = "@"  '<<<<<        
                        xlWorkSheet.Cells(i + 2, j + 1) = Me.DataGridView1(j, i).Value.ToString()


                    End If
                Next
            Next
        Next


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

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

        Dim res As MsgBoxResult
        res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
        If (res = MsgBoxResult.Yes) Then
            Process.Start("C:\Users\XXXX\Desktop\vbexcel.xlsx")
        End If

Open in new window

Avatar of melmers
melmers
Flag of Germany image

You must update your primary interop assemblies for you project to version 15. This can be done by
installing the  .NET Programmability Support with your Office setup and then referencing the new
PIA's which you need.

the other thing is have you installed the Visual Studio tools for Microsoft Office. You need VS 2012 upwards to support Office 2013,.
Avatar of Wilder1626

ASKER

Hi

So if i have Office 365 Business , i understand that it would not work. correct?
ASKER CERTIFIED SOLUTION
Avatar of melmers
melmers
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help, i installed Office 2014 instead of 365