Solved

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

Posted on 2013-05-25
1
1,044 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

773 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