Ejemplo n.º 1
0
        public ActionResult exporttoexcel(int listid)
        {
            using (XLWorkbook wb = new XLWorkbook())
            {
                var datalist = context.SpGetdataforexcelexport(listid).ToList();
                if (datalist != null && datalist.Count() > 0)
                {
                    var dt = ToDataTable(datalist);
                    wb.Worksheets.Add(dt);
                    wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    wb.Style.Font.Bold            = true;
                    Response.Clear();
                    Response.Buffer      = true;
                    Response.Charset     = "";
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

                    Random rdn      = new Random();
                    string FileName = "";

                    if (datalist.FirstOrDefault().City_Circle != null && datalist.FirstOrDefault().City_Circle != "")
                    {
                        FileName = datalist.FirstOrDefault().City_Circle + ".xlsx";
                    }
                    else
                    {
                        FileName = "Company.xlsx";
                    }


                    //Response.AddHeader("content-disposition", "attachment;filename= MemberDetails.xlsx");
                    Response.AddHeader("content-disposition", "attachment;filename= " + FileName);

                    using (MemoryStream MyMemoryStream = new MemoryStream())
                    {
                        wb.SaveAs(MyMemoryStream);
                        MyMemoryStream.WriteTo(Response.OutputStream);
                        Response.Flush();
                        Response.End();
                    }
                }
                else
                {
                    TempData["Message"] = "No Record Found to export";
                }
            }

            return(RedirectToAction("Index", new { listid = listid, lstname = "" }));
            // return View("Index");
        }