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.

That datagrid may have a header but your code only exports the data in the grid. The usual datagrid's "header" is a property most likely called HeaderText and is not shown in your code.

So I googled this one with https://www.google.com/search?q=export+datagrid+with+header

Second hit finds an example exporting the header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.