简体   繁体   中英

How to export filtered datagridview to excel using epplus

I can filter a date column in my datagridview but when i try to export it to excel it exports the whole datatable and not the filtered Datagridview.

These are part of what i have done so far

Filtering for the Date column which works

 private void BtnFilter_Click(object sender, EventArgs e)
    {
        bSource = new BindingSource();
        bSource.DataSource = eTable;
        dgv1.DataSource = bSource;
        bSource.Filter = string.Format("DateTime>= '{0:dd/MM/yyyy}' AND      DateTime <= '{1:dd/MM/yyyy}'", dtPickerStart.Value.Date, dtPickerStop.Value.Date);

Export event that exports whole datatable and not the filtered datagridview

}
                 using (ExcelPackage pck = new ExcelPackage(file))
                {
                    bSource = new BindingSource();
                    bSource.DataSource = dgv1;
                    dgv1.DataSource = eTable; 

                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1");
                    ws.Cells["A1"].LoadFromDataTable(((System.Data.DataTable)dgv1.DataSource), true, OfficeOpenXml.Table.TableStyles.Light1);      

I suspect that i am not using the bindingsource correctly.Any suggestions would be appreciated

Perhapse this post here helps: Get DataTable from DataGridView respecting filters and sorting

Try using

DataTable filtered = table.DefaultView.ToTable();

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