Solved

Export DataGridView1 to Excel 15.0

Posted on 2014-11-07
4
100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month6 days, 3 hours left to enroll

627 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