将所选列导出为Excel只会导出所选列,并且不会过滤或不选择数据 [英] Exporting selected columns to excel only exports selected columns and the data are not filtered or not selected

查看:108
本文介绍了将所选列导出为Excel只会导出所选列,并且不会过滤或不选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The Below is the code i used to filter the selected columns to excel. However, the contents (Data) that it exports in excel is not filtered only the column headernames. 

    Private Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
      Try
    
                Dim DTB = New DataTable, RWS As Integer, CLS As Integer
                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.Checked = True And Col.Name = PAGIBIG.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox3.Checked = True And Col.Name = PHILHEALTH.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox4.Checked = True And Col.Name = SSS.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox5.Checked = True And Col.Name = TIN.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox6.Checked = True And Col.Name = AgencyEmployeeNo.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox7.Checked = True And Col.Name = FirstName.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox8.Checked = True And Col.Name = MiddleName.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
                    If CheckBox9.Checked = True And Col.Name = LastName.Name Then
                        DTB.Columns.Add(Col.Name.ToString)
                    End If
    
                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) ' OPEN XML WITH EXCEL
    
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            '------------------------------------------------------Excel--------------------------------------------------------'
   
        End Sub
Here is the Datagridview with selected Columns that will be exported
http://imgur.com/E4fQ6GG
And here is the output of the excel file which the contents are not filtered only the headernames
http://imgur.com/Wed0Xha

Credits to :@Mederic for helping me previously





我尝试了什么:



最后我试过的是检查是否选中了关联的复选框,然后将col.name添加到数据表中,如上面的循环所示。



What I have tried:

The last thing i tried is that checking whether the assiciated checkbox is checked and the col.name then adding it to the datatable as seen in the loop above.

推荐答案

显然,你不懂你自己的代码。使用调试器查看它的执行情况。如果你想让你的代码过滤数据列,你必须告诉。



当你不明白你的代码在做什么或为什么它做它做的时候,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Obviously, you don't understand your own code. Use the debugger to see it perform. If you want your code to filter data columns, you have to tell.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于将所选列导出为Excel只会导出所选列,并且不会过滤或不选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆