Solved

Export DataGridView1 to Excel 15.0

Posted on 2014-11-07
4
98 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
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…

830 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