I found the solution on another site.I had to make use of the DataTable.DefaultView property to export the filtered datagridview.
This is the code to export the filterd datagridview
using (ExcelPackage pck = new ExcelPackage(file))
{
BindingSource bs = (BindingSource)dgv1.DataSource;
System.Data.DataTable table = (System.Data.DataTable)bs.DataSource;
System.Data.DataTable filtered = table.DefaultView.ToTable();
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sheet1");
ws.Cells["A1"].LoadFromDataTable(((System.Data.DataTable)filtered), true, OfficeOpenXml.Table.TableStyles.Light1);
using (ExcelRange rng = ws.Cells[1, 1, 1, dgv1.Columns.Count])
{
rng.Style.Font.Bold = true;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));
rng.Style.Font.Color.SetColor(System.Drawing.Color.White);
ws.Column(3).Hidden = false;
ws.Cells["A1"].AutoFitColumns(25);
ws.Cells["B1"].Value = "Employee Number";
ws.Cells["B1"].AutoFitColumns(40);
ws.Cells["B1"].Value = "Employee Number";
ws.Cells["D1"].AutoFitColumns(60);
ws.Column(4).Style.Numberformat.Format = "dd/MM/yyyy";
}
pck.Save();
}