Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Visual-Studio VB.NET
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.
 
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 30-Nov-12 20:42pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check out this It'll Work
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
  Permalink  
v2
Comments
armarzook at 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 at 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....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid