如何导出到dataGrid中由用户

时间:2017-07-03 11:57:09

标签: c# excel

我在c#应用程序中有一个dataTable,如下所示:

<DataGrid x:Name="dg1" Grid.Column="1" Grid.Row="0"  Grid.RowSpan="2"  SelectionMode="Extended" CanUserAddRows="False" ScrollViewer.VerticalScrollBarVisibility="Auto"  CanUserDeleteRows="False"  SelectedIndex="{Binding SelectedIndex}" VerticalAlignment="Top" AutoGenerateColumns="False"  IsReadOnly="True"    Margin="20,150,20,20" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="" Binding="{Binding no}" Visibility="Hidden" />
            <DataGridTextColumn Header="Issue ID" Binding="{Binding IssueID}" Width="0.4*" />
            <DataGridTextColumn Header="Summary" Binding="{Binding Summary}" Width="0.4*"/>
            <DataGridTextColumn Header="Type" Binding="{Binding Type}" Width="0.4*" />
            <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="0.4*"/>
            <DataGridTextColumn Header="Label" Binding="{Binding Label}"  Width="0.4*"/>
            <DataGridTextColumn Header="Components" Binding="{Binding Components}" Width="0.4*" />
            <DataGridTextColumn Header="Empty Fields" Binding="{Binding EmptyFields}" Width="0.4*" />
            <DataGridTextColumn Header="Reporter" Binding="{Binding Reporter}" Width="0.4*" />
            <DataGridTextColumn Header="Requester" Binding="{Binding Requester}" Width="0.4*" />
            <DataGridTextColumn Header="Requesting Team" Binding="{Binding RequestingTeam}" Width="0.4*" />
            <DataGridTextColumn Header="Assignee" Binding="{Binding Assignee}" Width="0.4*"/>

        </DataGrid.Columns>

    </DataGrid>

我通过下载一些jsons并解析它们来填充这个数据网格。我想将输出导出到Excel,但为用户提供一个选项,以选择要导出到Excel的列。任何人都可以提供代码或任何参考如何做到这一点。

1 个答案:

答案 0 :(得分:0)

自己想出解决方案。单击此表单上的导出到Excel格式按钮时,我打开了一个新表单。这个新表单包含表格每列的复选框和一个用于将复选框导出为Excel的按钮。我检查了按钮的onclick事件中的每个复选框,如果选中它们,则将相应的列添加到excel。