DataGridView Export with headers

Posted on 2012-08-18
Last Modified: 2012-08-19
Hi Experts

I am trying to get my datagridview to export with headers
I have tried to get it right but to no avail.

Private Sub BtnExport2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExport2.Click
 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 Int16, j As Int16

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

         For c As Integer = 0 To DataGridViewPerformance.Columns.Count - 1
            xlWorkSheet.Cells(1, c + 1).Value = DataGridViewPerformance.Columns(c).HeaderText

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

        xlWorkBook.SaveAs("c:\Performance.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
         Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
        xlWorkBook.Close(True, misValue, misValue)


        MessageBox.Show("Your File is successfully saved")
    End Sub

Open in new window

Can someone please take a look at this code and steer me right?

Question by:GrahamSA
    LVL 35

    Accepted Solution

    Your issue is that you are overriding the header line: (notice (i+2) change)
    Replace this line:
    xlWorkSheet.Cells(i + 1, j + 1) = DataGridViewPerformance(j, i).Value.ToString()

    xlWorkSheet.Cells(i + 2, j + 1) = DataGridViewPerformance(j, i).Value.ToString()

    Author Closing Comment

    Perfect Friend

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now