// GET: Reservation/ExportOrders
        public ActionResult ExportOrders()
        {
            IViewerFactory <OrderView> factory = new OrderViewFactory();

            var list = factory.Query(new Criteria(new
            {
                MenuDate = DateTime.Today,
                MenuType = (int)MenuDto.GetMenuLunchOrSupper(0, false)
            }));

            if (list != null && list.Count > 0)
            {
                var mapper = OrderDto.ConfigMapper().CreateMapper();
                var orders = mapper.Map <IEnumerable <OrderDto> >(list.AsEnumerable()).ToList();

                var book = ExcelManager.BuildOrderWorkbook(orders);

                byte[] file;
                using (var ms = new MemoryStream())
                {
                    book.Write(ms);
                    file = ms.GetBuffer();
                }

                return(File(file, "application/vnd.ms-excel", $@"今日订餐记录表-{DateTime.Today.ToString("yyyyMMdd")}.xls"));

                //return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                //    $"今日订餐记录表-{DateTime.Today.ToString("yyyyMMdd")}.xlsx");
            }

            return(RedirectToAction("TodayOrders", "Reservation"));
        }
Exemplo n.º 2
0
        // GET: Console/ExportOrders

        public ActionResult ExportOrders(string date)
        {
            IViewerFactory <OrderView> factory = new OrderViewFactory();

            DateTime         menuDate;
            List <OrderView> list;

            if (!string.IsNullOrEmpty(date) && DateTime.TryParse(date, out menuDate))
            {
                list = factory.Query(new Criteria(new { MenuDate = menuDate }));
            }
            else
            {
                // 为避免数据量过大,只显示7天内的订餐记录
                list = factory.Query(new Criteria
                {
                    WhereClause = "MenuDate >= DATEADD(DAY,-7,GETDATE())",
                    PagingSize  = 0
                });

                list.Sort((x1, x2) => x2.Menu.MenuDate.CompareTo(x1.Menu.MenuDate));
            }

            if (list != null && list.Count > 0)
            {
                var mapper = OrderDto.ConfigMapper().CreateMapper();
                var orders = mapper.Map <IEnumerable <OrderDto> >(list.AsEnumerable()).ToList();

                var book = ExcelManager.BuildOrderWorkbook(orders);

                byte[] file;
                using (var ms = new MemoryStream())
                {
                    book.Write(ms);
                    file = ms.GetBuffer();
                }

                var timestamp = !string.IsNullOrEmpty(date)
                    ? Convert.ToDateTime(date).ToString("yyyyMMdd")
                    : string.Empty;

                return(File(file, "application/vnd.ms-excel", $@"订餐记录表-{timestamp}.xls"));

                //return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                //    $@"订餐记录表-{timestamp}.xlsx");
            }

            return(RedirectToAction("OrderManagement", "Console", new { date }));
        }