Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


DataGridView Export with headers

Posted on 2012-08-18
Medium Priority
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 36

Accepted Solution

Miguel Oz earned 2000 total points
ID: 38308560
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

ID: 38308974
Perfect Friend

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

580 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