Solved

Export VB.NET DataViewGrid data and append to existing Excel spreadsheet

Posted on 2013-05-25
1
1,030 Views
Last Modified: 2013-05-28
Hello again Experts,

I'm attempting to export data from a VB.NET dataViewGrid and export this data to Excel.  I'm able to export the contents of the dvg just fine, but appending the data to existing data in the Excel spreadsheet has proven to be a bit of a challenge.  The code examples I've seen during my internet search have not proven to be correct so far.

Here is what I have.   Thanks in advance for your help.

  Public Sub ExportToExcel(ByVal dgv As DataGridView, ByVal path As String)

        Dim xlApp As New Excel.Application
        'create a new workbook
        Dim xlWb As Excel.Workbook = xlApp.ActiveWorkbook
        Dim xlWs As Excel.Worksheet
        Dim lastRow As Long
        Dim lastCol As Long
       
       
        'Check for existing data
        With xlApp
            .Visible = True
            'open workbook
            xlWb = .Workbooks.Open(path)

            'set it to the relavant sheet
            xlWs = xlWb.ActiveSheet

            With xlWs
                Dim rowcount As Integer = 1

                For Each gridRow As DataGridViewRow In dgv.Rows
                        rowcount += 1
                    For i As Integer = 0 To dgv.Columns.Count - 1
                        'Add the header the first time through
                        If rowcount = 2 Then
                            .Cells(1, i + 1).Value = dgv.Columns(i).HeaderText
                            .Cells(1, i + 1).font.bold = True

                            'export the values to a blank Spreadsheet
                            For rowlength As Integer = 0 To dgv.Rows.Count - 1
                                For columnlength As Integer = 0 To dgv.Columns.Count - 1
                                    .Cells(rowlength + 2, columnlength + 1).Value = dgv.Rows(rowlength).Cells(columnlength).Value
                                Next
                            Next

                            'append values to a previously populated Spreadsheet
                            If rowcount > 2 Then
                                lastRow = xlWs.Cells.Find(What:="*", After:=xlApp.Range("A1"), SearchOrder:=Excel.XlSearchOrder.xlByColumns, _
                                SearchDirection:=Excel.XlSearchDirection.xlPrevious).Row
                                For rowlength As Integer = 0 To dgv.Rows.Count - 1
                                    For columnlength As Integer = 0 To dgv.Columns.Count - 1
                                        .Cells(lastRow, columnlength + 1).Value = dgv.Rows(rowlength).Cells(columnlength).Value
                                    Next
                                Next
                            End If
                        End If

                    Next



                Next

            End With



        End With


       
       
        'save the workbook as the path specified
        xlWb.SaveAs(path)
        

        releaseObject(xlApp)

    End Sub

Open in new window

0
Comment
Question by:brohjoe
1 Comment
 
LVL 9

Accepted Solution

by:
suvmitra earned 500 total points
ID: 39197374
grid view export to excel provides you option to save / export the data in a new workbook. This is how it works.

If you want to write / append new data in excel file then you need to use .net stream writer class. Hope it helps.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

911 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

21 Experts available now in Live!

Get 1:1 Help Now