UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Export Excel From Vb.net Datagridview With Column Headers    
 
   
rlsdata
post Jan 27 2011, 11:15 PM
Post #1

UtterAccess Veteran
Posts: 477
From: Michgan, USA



I am using this code i found from links on this site to export data grid information to excel. it works great! I am trying to figure out how to first write the column headers to excel and then the data in the columns.


CODE
Private Sub exportExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exportExcel.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 Integer
        Dim j As Integer

        xlApp = New excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For Each col As DataGridViewColumn In Me.BurnPermitInformationDataGridView.Columns
            xlWorkSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
        Next
        For i = 2 To Me.BurnPermitInformationDataGridView.Rows.Count - 1
            For j = 0 To Me.BurnPermitInformationDataGridView.ColumnCount
                xlWorkSheet.Cells(i + 1, j + 1) = Me.BurnPermitInformationDataGridView(j, i).Value.ToString()
            Next
        Next

        xlWorkBook.Activate()

        '//get path
        Me.FolderBrowserDialog1.ShowDialog()
        Dim path As String = Me.FolderBrowserDialog1.SelectedPath

        xlWorkBook.SaveAs(path & "\burn permit export.xls")
        'xlWorkSheet.SaveAs("burn permit export.xls")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("You can find your report at " & path & "\burn permit export.xls")
    End Sub


This is specifically where I am trying to write the header text followed by the data in the columns:

CODE
For Each col As DataGridViewColumn In Me.BurnPermitInformationDataGridView.Columns
            xlWorkSheet.Cells(1, col.Index + 1) = col.HeaderText.ToString
        Next
        For i = 2 To Me.BurnPermitInformationDataGridView.Rows.Count - 1
            For j = 0 To Me.BurnPermitInformationDataGridView.ColumnCount
                xlWorkSheet.Cells(i + 1, j + 1) = Me.BurnPermitInformationDataGridView(j, i).Value.ToString()
            Next
        Next


I dont seem to get any values with col.HeaderText.Tostring
Go to the top of the page
 
+
Luiz_Cláudio
post Mar 5 2011, 09:27 PM
Post #2

UtterAccess VIP
Posts: 722
From: São Paulo, Brazil



Hi Ryan,

I had no problem retrieving header text by using this code:

CODE
For Each col As DataGridViewColumn In Me.DataGridView1.Columns
    MessageBox.Show(col.HeaderText.ToString)
Next
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 20th June 2013 - 01:30 AM

Tag cloud: