Exemple #1
0
 public ActionResult ListViewCart()
 {
     if ((int?)Session["UserID"] != null)
     {
         List <BillDetail> a = new List <BillDetail>();
         a = BillAction.ListCartDetail((int)Session["UserID"]);
         ViewBag.CountItem = BillAction.CountBill((int)Session["UserID"]);
         ViewBag.Total     = BillAction.ReBill((int)Session["UserID"]);
         if (a != null)
         {
             return(PartialView(a));
         }
         else
         {
             return(PartialView());
         }
     }
     else if (BillAction.CountBill((int)Session["UserID"]) == 0)
     {
         return(RedirectToAction("HomePage", "Home"));
     }
     else
     {
         return(RedirectToAction("Login", "Account"));
     }
 }
        public ActionResult ExportToExcel(int id)
        {
            var            products = BillAction.ListBillDetail(id);
            var            bill     = BillAction.ReBill(id);
            ExcelPackage   pck      = new ExcelPackage();
            ExcelWorksheet ws       = pck.Workbook.Worksheets.Add("Report");

            ws.Cells["A1:D1"].Value = "Thông Tin Đơn hàng";
            ws.Cells["A1:D1"].Merge = true;
            ws.Cells["A1:D1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            //ws.Cells["A1:D1"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["A3"].Value = "Người đặt hàng";
            ws.Cells["A3"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            // ws.Cells["A3"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["B3"].Value    = bill.Account.UserName;
            ws.Cells["B3:D3"].Merge = true;
            ws.Cells["B3:D3"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            //  ws.Cells["B3:D3"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["A4"].Value = "Ngày đặt hàng";
            ws.Cells["A4"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            // ws.Cells["A4"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["B4"].Value    = string.Format("{0:dd MMMM yyyy} at {0:H: mm tt}", bill.FoundedDate);
            ws.Cells["B4:D4"].Merge = true;
            ws.Cells["B4:D4"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            //  ws.Cells["B4:D4"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["A6"].Value = "STT";
            ws.Cells["A6"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells["A6"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["B6"].Value = "Tên sách";
            ws.Cells["B6"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells["B6"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["C6"].Value = "Số lượng";
            ws.Cells["C6"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells["C6"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            ws.Cells["D6"].Value = "Giá sau giảm";
            ws.Cells["D6"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells["D6"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

            int rowStart = 7;
            int stt      = 1;

            foreach (var item in products)
            {
                if (item.Count < 10)
                {
                    //    ws.Cells[string.Format("A{0}:D{1}",rowStart,rowStart)].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    //    ws.Cells[string.Format("A{0}:D{1}", rowStart, rowStart)].Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml(string.Format("pink")));
                }

                ws.Cells[string.Format("A{0}", rowStart)].Value = stt.ToString();
                ws.Cells[string.Format("A{0}", rowStart)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[string.Format("A{0}", rowStart)].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                ws.Cells[string.Format("B{0}", rowStart)].Value = item.Book.Name;
                ws.Cells[string.Format("B{0}", rowStart)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[string.Format("B{0}", rowStart)].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                ws.Cells[string.Format("C{0}", rowStart)].Value = item.Count;
                ws.Cells[string.Format("C{0}", rowStart)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[string.Format("C{0}", rowStart)].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                ws.Cells[string.Format("D{0}", rowStart)].Value = item.Price;
                ws.Cells[string.Format("D{0}", rowStart)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells[string.Format("D{0}", rowStart)].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                rowStart++;
                stt++;
            }

            ws.Cells[string.Format("A{0}:C{1}", rowStart, rowStart)].Value = "TỔNG TIỀN THANH TOÁN:";
            ws.Cells[string.Format("A{0}:C{1}", rowStart, rowStart)].Merge = true;
            ws.Cells[string.Format("A{0}:C{1}", rowStart, rowStart)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells[string.Format("A{0}:C{1}", rowStart, rowStart)].Style.Border.BorderAround(ExcelBorderStyle.Thin);
            ws.Cells[string.Format("A{0}:C{1}", rowStart, rowStart)].Style.Font.Bold = true;

            ws.Cells[string.Format("D{0}", rowStart)].Value = bill.TotalCost;
            ws.Cells[string.Format("D{0}", rowStart)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells[string.Format("D{0}", rowStart)].Style.Border.BorderAround(ExcelBorderStyle.Thin);
            ws.Cells[string.Format("D{0}", rowStart)].Style.Font.Bold = true;
            int rowEnd = rowStart + 2;

            ws.Cells[string.Format("A{0}:D{1}", rowEnd, rowEnd)].Value = "XIN CẢM ƠN QUÝ KHÁCH!";
            ws.Cells[string.Format("A{0}:D{1}", rowEnd, rowEnd)].Merge = true;
            ws.Cells[string.Format("A{0}:D{1}", rowEnd, rowEnd)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            ws.Cells[string.Format("A{0}:D{1}", rowEnd, rowEnd)].Style.Border.Top.Style    = ExcelBorderStyle.DashDot;

            ws.Cells["A:AZ"].AutoFitColumns();
            Response.Clear();
            Response.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attactment: filename=" + "ExcelReport.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.End();

            return(View());
        }