protected void btnExecute_Click(object sender, EventArgs e) { //Define a workbook to store null value initially Workbook workbook = null; string path = MapPath("."); path = path.Substring(0, path.LastIndexOf("\\")); ProductsList productsList = new ProductsList(path); //Create a workbook based on a class's custom method workbook = productsList.CreateProductsList(); if (ddlFileVersion.SelectedItem.Value == "XLS") { ////Save file and send to client browser using selected format workbook.Save(HttpContext.Current.Response, "ProductList.xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003)); } else { workbook.Save(HttpContext.Current.Response, "ProductList.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx)); } //end response to avoid unneeded html HttpContext.Current.Response.End(); }