Solved

Export DataGridView1 to Excel 15.0

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now