public ActionResult SalesOfEachQuarter(DateTime?date) { if (date.GetValueOrDefault().Month == 0) { ModelState.AddModelError("", "You must input month !!!"); } else { ListReports list = new ListReports(); Session["date"] = date; var model = list.SalesOfEachQuarter(date.GetValueOrDefault()); ViewBag.Date = list.FirstDayOfQuarter(date.GetValueOrDefault()) + " - " + list.LastDayOfQuarter(date.GetValueOrDefault()); return(View(model)); } return(View()); }
public FileResult ExportSalesOfEachQuarter() { DataTable dt = new DataTable("Grid"); dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Key"), new DataColumn("Amount $"), new DataColumn("Min $"), new DataColumn("Max $"), new DataColumn("AVG $") }); try { if (Session["date"].ToString() != "") { ListReports list = new ListReports(); DateTime date = (DateTime)Session["date"]; var model1 = list.SalesOfEachQuarter(date); foreach (var rp in model1) { dt.Rows.Add(rp.Id, rp.Amount, rp.Min, rp.Max, rp.AVG); } using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt); using (MemoryStream stream = new MemoryStream()) { wb.SaveAs(stream); return(File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "SalesOfEachQuarter from " + list.FirstDayOfQuarter(date) + " to " + list.LastDayOfQuarter(date) + " - " + DateTime.Now + ".xlsx")); } } } var model = new ListReports().SalesOfEachQuarter(); foreach (var rp in model) { dt.Rows.Add(rp.Id, rp.Amount, rp.Min, rp.Max, rp.AVG); } using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt); using (MemoryStream stream = new MemoryStream()) { wb.SaveAs(stream); return(File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "SalesOfEachQuarter - " + DateTime.Now + ".xlsx")); } } } catch (Exception) { var model = new ListReports().SalesOfEachQuarter(); foreach (var rp in model) { dt.Rows.Add(rp.Id, rp.Amount, rp.Min, rp.Max, rp.AVG); } using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt); using (MemoryStream stream = new MemoryStream()) { wb.SaveAs(stream); return(File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "SalesOfEachQuarter - " + DateTime.Now + ".xlsx")); } } } }