Esempio n. 1
0
        public ActionResult exporttocontactexcel(int listid, string biztype)
        {
            using (XLWorkbook wb = new XLWorkbook())
            {
                var datalist = context.Spcontexportclient(listid, biztype).ToList();
                var dt       = ToDataTable(datalist);
                if (dt.Rows.Count > 0)
                {
                    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 = "ContactList_" + datalist.FirstOrDefault().City_Circle + ".xlsx";
                    }
                    else
                    {
                        FileName = "ContactList_.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 Contact Found to export";
                }
            }

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