将datagridview列导出为ex​​cel

时间:2013-11-20 16:59:40

标签: vb.net

在我的datagridview中我有第一列visible = false。我需要将此列导出为ex​​cel

我有以下代码导出到excel:

If Sfd.ShowDialog() = DialogResult.OK Then

            Dim App As New Excel.Application
            Dim WB As Excel.Workbook
            Dim WS As New Excel.Worksheet

            WB = App.Workbooks.Add()

            WS = WB.ActiveSheet

            For i As Integer = 1 To DG.Columns.Count
                WS.Cells(1, i) = DG.Columns(i - 1).HeaderText
            Next

            For i As Integer = 0 To DG.Rows.Count - 1
                For j As Integer = 0 To DG.Columns.Count - 1
                    WS.Cells(i + 2, j + 1) = DG.Rows(i).Cells(j).Value.ToString()
                    WS.Cells(i + 2, 1).Font.Color = Color.Blue
                Next
            Next

            With WS
                With .Range(.Cells(1, 1), .Cells(1, DG.ColumnCount)).Font
                    .Color = Color.White
                    .Bold = 1
                    .Size = 12
                End With
                .Range(.Cells(1, 1), .Cells(1, DG.ColumnCount)).Interior.Color = Color.Black
                .Columns.AutoFit()
                .Columns.HorizontalAlignment = 2
            End With

            WB.SaveAs(Sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal)

            WB.Close()
            Process.Start(Sfd.FileName)
        End If

由于

4 个答案:

答案 0 :(得分:1)

  For i = 0 To DG.Columns.Count - 1
     WS.Cells(1, i + 1) = DG.Columns(i).HeaderText
  Next

答案 1 :(得分:0)

为什么不首先生成可以在不添加不想导出的列的情况下构建的数据表,然后可以将相同的导出过程应用于此数据表。

答案 2 :(得分:0)

如下所示更改for循环:

  For i As Integer = 1 To DG.Columns.Count-1
                WS.Cells(1, i) = DG.Columns(i).HeaderText
  Next

 For i As Integer = 0 To DG.Rows.Count - 1
                For j As Integer = 1 To DG.Columns.Count - 1
                    WS.Cells(i + 2, j + 1) = DG.Rows(i).Cells(j).Value.ToString()
                    WS.Cells(i + 2, 1).Font.Color = Color.Blue
                Next
 Next

我刚刚从第二列改变了内部for循环开始。因此它不会导出第一列

答案 3 :(得分:0)

您好我有同样的问题尝试下面的代码它在我的应用程序中工作得很好 这是我复制代码的链接 Exporting displayed columns in dataGridView to Excel

' creating Excel Application

        If ((dgSMS.Columns.Count = 0) Or (dgSMS.Rows.Count = 0)) Then Exit Sub
        Dim XlApp = New Excel.Application With {.Visible = True}
        XlApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet)
        Dim xlWS = XlApp.ActiveSheet
        xlWS.Name = "Exported Data"

        'Copy visible data from DGV to Excel
        Dim columnCollection As DataGridViewColumnCollection = dgSMS.Columns
        Dim currentVisibleColumn As DataGridViewColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
        Dim lastColumnExported As DataGridViewColumn = currentVisibleColumn
        Dim visibleColumnCount As Integer = columnCollection.GetColumnCount(DataGridViewElementStates.Visible)

        'Finally export the data
        For c = 1 To visibleColumnCount
            xlWS.Cells(1, c) = currentVisibleColumn.HeaderText
            currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
            lastColumnExported = currentVisibleColumn
        Next

        'Only export visible cells
        For r = 0 To dgSMS.Rows.Count - 1
            'Reset values
            currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
            lastColumnExported = currentVisibleColumn
            For c = 1 To visibleColumnCount
                Dim value = dgSMS.Rows(r).Cells(currentVisibleColumn.Index).Value
                If value <> vbNullString Then
                    xlWS.Cells(r + 2, c) = value.ToString()
                End If
                currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
                lastColumnExported = currentVisibleColumn
            Next
        Next

        'Autosize columns in excel
        Dim columns = xlWS.UsedRange.Columns
        columns.AutoFit()


        ' save the application

        'workbook.SaveAs("c:\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
        '  Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing)



        ' Exit from the application

        ' app.Quit()