public static void ExportToExcel <T>(string workSheetName, Controller controller, DataManager dataManager) { var select = dataManager.GetSelect <T>(); var dataTable = Repos <T> .Table(select.Select(x => true)); using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dataTable, workSheetName); string myName = controller.Server.UrlEncode("Test" + "_" + DateTime.Now.ToShortDateString() + ".xlsx"); MemoryStream stream = GetStream(wb);// The method is defined below controller.Response.Clear(); controller.Response.Buffer = true; controller.Response.AddHeader("content-disposition", "attachment; filename=" + myName); controller.Response.ContentType = "application/vnd.ms-excel"; controller.Response.BinaryWrite(stream.ToArray()); controller.Response.End(); } }