Exemplo n.º 1
0
        public FileResult Export()
        {
            ShopNuocHoaEntities entities = new ShopNuocHoaEntities();
            DataTable           dt       = new DataTable("Grid");

            dt.Columns.AddRange(new DataColumn[6] {
                new DataColumn("Mã Đơn Hàng"),
                new DataColumn("Tên khách hàng"),
                new DataColumn("Ngày đặt hàng"),
                new DataColumn("Ngày giao hàng"),
                new DataColumn("Trị giá"),
                new DataColumn("Tình trạng giao hàng")
            });
            int i         = int.Parse(Session["month"].ToString());
            int i1        = int.Parse(Session["year"].ToString());
            int tong      = int.Parse(Session["tongtien"].ToString());
            var customers = from customer in entities.DonHangs.Where(n => n.NgayDat.Value.Month == i && n.NgayDat.Value.Year == i1 && n.TinhTrangGiaoHang == true)
                            select customer;

            Session["excel"] = customers;
            foreach (var customer in customers)
            {
                if (customer.TinhTrangGiaoHang == true)
                {
                    dt.Rows.Add(customer.MaDonHang, customer.TenNguoiNhan, customer.NgayGiao, customer.NgayGiao, customer.TriGia, customer.TinhTrangGiaoHang);
                }
                else
                {
                    dt.Rows.Add(customer.MaDonHang, customer.TenNguoiNhan, customer.NgayGiao, customer.NgayGiao, customer.TriGia, "Đang giao");
                }
            }
            dt.Rows.Add("Tổng tiền:", tong);
            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", "Grid.xlsx"));
                }
            }
        }