0

I export datagrid to excel file using this below code. But it doesn't export datagrid header. I'm just change values at for loop. But it doesn't help me. Please help me.

 Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For i = 0 To MetroGrid1.RowCount - 2
            For j = 0 To MetroGrid1.ColumnCount - 1
                xlWorkSheet.Cells(i + 1, j + 1) = _
                  MetroGrid1(j, i).Value.ToString()
            Next
        Next

        '**************

        Dim sd As New SaveFileDialog 'declare save file dialog
        If sd.ShowDialog = Windows.Forms.DialogResult.OK Then 'check if save file dialog was close after selecting a path
            'MsgBox(sd.FileName)
            xlWorkSheet.SaveAs(sd.FileName & ".xlsx") 'sd.filename reurns save file dialog path
            xlWorkBook.Close()
            xlApp.Quit()
        End If

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

and I wanted to describe answer about for loop code snip of above. I mean that how that loop work.
ThankYOu.

2
Contributors
1
Reply
10
Views
5 Months
Discussion Span
Last Post by rproffitt
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.