将datagridview标题列导出为ex​​cel vb 2012

时间:2015-05-26 11:33:49

标签: vb.net excel datagridview

我使用visual studio 2012和microsoft SQL server 2012使用此编码将datagridview导出到excel:

公共类别出口

 Private Sub Export_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.AllowUserToAddRows = False
        ClassKoneksi.namadatabase = "KPIRWAN"
        Dim dssiswa As New DataSet
        Dim sql As String
        sql = "select*from Siswa order by NIS ASC"
        dssiswa = ClassSiswa.displayData(ClassSiswa.opencon, sql, "DataSiswa")
        DataGridView1.DataSource = dssiswa
        DataGridView1.DataMember = "DataSiswa"
        DataGridView1.ReadOnly = True
        ClassSiswa.closecon()
    End Sub
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)

            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally

            GC.Collect()

        End Try
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ExportExcel()
    End Sub

    Private Sub ExportExcel()
        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim oValue As Object = System.Reflection.Missing.Value
        Dim sPath As String = String.Empty
        Dim dlgSave As New SaveFileDialog
        dlgSave.DefaultExt = "xls"
        dlgSave.Filter = "Microsoft Excel|*.xls"
        dlgSave.InitialDirectory = Application.StartupPath

        If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                xlApp = New Microsoft.Office.Interop.Excel.Application
                xlBook = xlApp.Workbooks.Add(oValue)
                xlSheet = xlBook.Worksheets("sheet1")

                Dim xlRow As Long = 2
                Dim xlCol As Short = 1

                For k As Integer = 0 To DataGridView1.ColumnCount - 1
                    xlSheet.Cells(1, xlCol) = DataGridView1(k, 0).Value
                    xlCol += 1

                Next

                For k As Integer = 0 To DataGridView1.ColumnCount - 1
                    xlSheet.Cells(2, xlCol) = DataGridView1(k, 0).Value
                    xlCol += 1

                Next

                For i As Integer = 0 To DataGridView1.RowCount - 1
                    xlCol = 1

                    For k As Integer = 0 To DataGridView1.ColumnCount - 1
                        xlSheet.Cells(xlRow, xlCol) = DataGridView1(k, i).Value
                        xlCol += 1

                    Next

                    xlRow += 1

                Next

                xlSheet.Columns.AutoFit()
                Dim sFileName As String = Replace(dlgSave.FileName, ".xlsx", "xlx")

                xlSheet.SaveAs(sFileName)
                xlBook.Close()
                xlApp.Quit()

                releaseObject(xlApp)
                releaseObject(xlBook)
                releaseObject(xlSheet)

                MsgBox("Data successfully exported.", MsgBoxStyle.Information, "PRMS/SOB Date Tagging")
            Catch
                MsgBox(ErrorToString)
            Finally
            End Try
        End If
    End Sub
End Class

它工作得非常好,除非我将datagridview导出为ex​​cel时,datagridview中的列标题文本不会导出到excel工作表中仅仅是gridview。

如何进行编码以使列标题文本出现在Excel表格中?

1 个答案:

答案 0 :(得分:0)

我认为你必须从每一栏获得HeaderText,如下所示:

xlSheet.Cells(x,y).Value = DataGridView1.Columns(k).HeaderText

你会把它放在你的“k”循环中。然后在文件中的任何位置写下它。 xy必须是您编写标题的位置(可能由k确定)

编辑:在excel的第一行写标题

For k As Integer = 0 To DataGridView1.ColumnCount - 1
    xlSheet.Cells(1,k+1).Value = DataGridView1.Columns(k).HeaderText
Next