Sum Up all the Data in a Column and Display it Below VB.Net and Excel
<p>Good Afternoon to all</p>

<p>I populate data in Datagridview from mySQL like this.</p>

<p><a href="http://i.stack.imgur.com/NtlHu.jpg" rel="nofollow">Datagridview Data</a></p>

<p>the next thing I do is that I have an Export Button and if I Click that it will Export the Data from Datagridview in Excel like this</p>

<p><a href="http://i.stack.imgur.com/rD9Kw.jpg" rel="nofollow">Extracted in Excel</a></p>

<p>My Question is How can I Find the Last Data in Column "Total" and Put the Sum below that? As of now the Image shows only two rows in excel but someday it will populate, I just want to Sum up all the data in the Column "Total" and Display the Output in below the last Data. I hope you help me. :(</p>

<p>TYSM </p>

<p>by the way here is my code</p>

<pre><code>If DataGridView1.Rows.Count = 0 Then
        MsgBox("Nothing to Export")
    Else
        Dim ExcelApp As Object, ExcelBook As Object
        Dim ExcelSheet As Object
        Dim i As Integer
        Dim j As Integer
        Dim exl As Excel.Application


        Dim NewWorksheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim myRange As Microsoft.Office.Interop.Excel.Range
        ExcelApp = CreateObject("Excel.Application")
        ExcelBook = ExcelApp.WorkBooks.Add
        ExcelSheet = ExcelBook.WorkSheets(1)

        exl = New Excel.Application
        exl.Visible = True
        With ExcelSheet
            For Each column As DataGridViewColumn In DataGridView1.Columns

                .cells(1, column.Index + 1) = column.HeaderText
            Next
            For i = 1 To Me.DataGridView1.RowCount
                .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
                For j = 1 To DataGridView1.Columns.Count - 1
                    .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
                    .Cells(4, 13) = "Grand Total"
                    .Cells(4, 14).Formula = "=SUM(Sheet1!$J2:$J1048576)"
                Next
            Next
        End With
        NewWorksheet = DirectCast(ExcelBook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
        myRange = NewWorksheet.Range("A:K")
        myRange.Font.Bold = True
        myRange.Font.Size = 9
        myRange.Font.FontStyle = "Calibri"
        ExcelSheet.Rows.Item(1).EntireColumn.AutoFit()
        ExcelApp.Visible = True
        ExcelSheet = Nothing
        ExcelBook = Nothing
        ExcelApp = Nothing
    End If
</code></pre>