简体   繁体   中英

Web Form DataGrid export to excel using EPPlus

I have a Web Form application that has a DataGrid and hides a few columns depending on certain conditions. When I export just the headers I am also exporting the columns I am hiding in my code behind. How can I export the columns the current user is seeing?

I figured it out. I am converting my datagrid to a datatable because I need to work with it later on. The if clause inside the foreach only looks for visible columns.

private DataTable ConvertToDataTable(DataGrid dataGrid)
        {
            var dt = new DataTable();
            foreach (DataGridColumn dgCol in dataGrid.Columns)
            {
                if (dgCol.Visible)
                {
                    dt.Columns.Add(dgCol.HeaderText);
                }
            }

            return dt;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM