Solved

Export DataGridView1 to Excel 15.0

Posted on 2014-11-07
4
95 Views
Last Modified: 2014-12-02
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

0
Comment
Question by:Wilder1626
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:melmers
ID: 40466270
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,.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 40471876
Hi

So if i have Office 365 Business , i understand that it would not work. correct?
0
 
LVL 11

Accepted Solution

by:
melmers earned 500 total points
ID: 40471931
I have no Office 365 but a wuick google scan shows that with the pia version 15 it could be possible
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 40475606
Thanks for your help, i installed Office 2014 instead of 365
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now