仅将datagridview中的可见列导出到Excel

时间:2017-04-12 08:07:48

标签: excel vb.net datagridview datagrid

我目前正在开发我的OJT所需的软件。我试图将我的数据网格导出到Excel文件。问题是即使我把复选框设置为特定列的可见性为false并且它隐藏在我的数据网格上但是当生成excel文件时,列仍然可见。继承我的代码,谢谢你。

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TabPage1.Enabled = False
        TabPage2.Enabled = False
        'Form3.Show()'


        DATAGRIDVIEW_TO_EXCEL((DataGridView1)) 
    End Sub
    '-------------------------------------------------Excel------------------------------------------------'
    Private Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
        Try
            Dim DTB = New DataTable, RWS As Integer, CLS As Integer
                For CLS = 0 To DGV.ColumnCount - 1 
                    DTB.Columns.Add(DGV.Columns(CLS).Name.ToString)
                Next

                Dim DRW As DataRow

                For RWS = 0 To DGV.Rows.Count - 1 
                    DRW = DTB.NewRow

                    For CLS = 0 To DGV.ColumnCount - 1
                        Try
                            DRW(DTB.Columns(CLS).ColumnName.ToString) = DGV.Rows(RWS).Cells(CLS).Value.ToString
                        Catch ex As Exception

                        End Try
                    Next

                    DTB.Rows.Add(DRW)
                Next

                DTB.AcceptChanges()

                Dim DST As New DataSet
                DST.Tables.Add(DTB)
                Dim FLE As String = "E:\Export\Export.xml" 
                DTB.WriteXml(FLE)
                Dim EXL As String = "C:\Program Files\Microsoft Office\Office15\EXCEL.exe" 
                Shell(Chr(34) & EXL & Chr(34) & " " & Chr(34) & FLE & Chr(34), vbNormalFocus) 

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        '------------------------------------------------------Excel--------------------------------------------------------'



    End Sub

以下是我隐藏列的方法

   Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            Me.GSIS.Visible = False
        Else
            Me.GSIS.Visible = True
        End If

    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked = True Then
            Me.PAGIBIG.Visible = False
        Else
            Me.PAGIBIG.Visible = True
        End If

    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked = True Then
            Me.PHILHEALTH.Visible = False
        Else
            Me.PHILHEALTH.Visible = True
        End If
    End Sub


    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
        If CheckBox4.Checked = True Then
            Me.SSS.Visible = False
        Else
            Me.SSS.Visible = True
        End If
    End Sub

    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox5.Checked = True Then
            Me.TIN.Visible = False
        Else
            Me.TIN.Visible = True
        End If
    End Sub

    Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox6.Checked = True Then
            Me.AgencyEmployeeNo.Visible = False
        Else
            Me.AgencyEmployeeNo.Visible = True
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

我会改变这个:

'For Every Column in the DataGridView
For Each Col as DataGridViewColumn in DGV.Columns
   If Checkbox1.checked = True AND Col.Name = GSIS.Name Then
       DTB.Columns.Add(Col.Name.ToString)
   End If
   'If checkbox2 etc etc 
Next

对于这样的事情:

for i in range(len(data)):
    if data[i]==nan:
        data[i]=0

理想所以你不必复制粘贴这么多就是创建一个类,你可以为每个col关联一个可见性属性,并只在循环中检查该属性

/!\小心/!\

我添加 Col.Nale = GSIS.Name ,假设 GSIS ,但在您的代码中可能不是这种情况。因此,请尝试了解代码,而不是复制粘贴。