Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Coders,

I managed to export the data of a datagridview to an Excel File but i can't able to fetch the column names of the datagridview. Please help me with this. The Code is below.

VB
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer

'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)

With ExcelSheet
    For i = 1 To Me.DataGridView1.RowCount
        .cells(i, 1) = Me.DataGridView1.Rows(i - 1).Cells("sl_no").Value
        For j = 1 To DataGridView1.Columns.Count - 1
            .cells(i, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
        Next
    Next
End With

ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
Posted

Check out this It'll Work
VB
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
 
'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
 
With ExcelSheet
    ' Starting Loop on Column Headers to Export in Excel
    For i = 1 To Me.DataGridView1.ColumnCount
        .Cells(1, i) = DataGridView1.Columns(i - 1).HeaderText
    Next
    ' Starting to Export Datas in Grid to Excel
    For i = 1 To Me.DataGridView1.RowCount
        For j = 0 To DataGridView1.Columns.Count - 1
            ' Here I use "i + 1" to Define Rows,
            ' Because Cells of Row "i" is already used to Store Column Header Data...
            .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
        Next
    Next
End With
 
ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
 
Share this answer
 
v2
Comments
armarzook 4-Dec-12 7:41am    
Thank You for the solution. This Works. But as a noob i would like to know how actually this one works. Can u explain it?
Ashok19r91d 4-Dec-12 22:55pm    
Yeah Sure, After reading Your code, I hope that You are a VB6 or Earlier Version Programmer, Using FlexGrid or Equivalent controls to Load and Print Data, Now You're Shifting to .Net... Right Now, In Classic VB Both FixedRows and NonFixedRows or termed as Rows, So Looping through Rows Properties helps To Print Everything including Headers...
But in .Net Rows and Column Headers are Separate Objects, So For Printing Column Headers You've to Loop through Column Headers before Starting Loop in Row Object...
Now I've Improve my Solution with Comments, Go Through Comments I'll help you understand Better....
Look below articles,the first uses two ways to export data to Excel through data gridview,also another article, which has 9 solutions to export different data to Excel including datagridview, both is helpful to you:
Excel to Datatable and Datatable to Excel[^]
9 Solutions to Export Data to Excel for ASP.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900