Exemplo n.º 1
0
        public async Task <IActionResult> OnPostExport(int currentPage, int pageSize, string startDate, string endDate, string customer, string form, string servant, string status, string contactCustomer)
        {
            Feedbacks = await InitiateView(currentPage, pageSize, startDate, endDate, customer, form, servant, status, contactCustomer);

            string   sWebRootFolder = _hostingEnvironment.WebRootPath;
            string   sFileName      = @"Orders Data.xlsx";
            string   URL            = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
            FileInfo file           = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
            var      memory         = new MemoryStream();

            using (var fs = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Create, FileAccess.Write))
            {
                ExcelPackage package = new ExcelPackage();
                package.Workbook.Worksheets.Add("Orders");
                ExcelWorksheet orderSheet = package.Workbook.Worksheets["Orders"];
                using (var range = orderSheet.Cells["A1:N1"])
                {
                    range.Style.Font.Bold        = true;
                    range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(23, 121, 186));
                    range.Style.Font.Color.SetColor(Color.White);
                }
                orderSheet.Cells[1, 1].Value  = "Order Status";
                orderSheet.Cells[1, 2].Value  = "Order Id";
                orderSheet.Cells[1, 3].Value  = "Payment Status";
                orderSheet.Cells[1, 4].Value  = "Payment Method";
                orderSheet.Cells[1, 5].Value  = "Order Date";
                orderSheet.Cells[1, 6].Value  = "Paid";
                orderSheet.Cells[1, 7].Value  = "Change";
                orderSheet.Cells[1, 8].Value  = "Order Discount";
                orderSheet.Cells[1, 9].Value  = "Sub Total";
                orderSheet.Cells[1, 10].Value = "VAT Total";
                orderSheet.Cells[1, 11].Value = "Grand Total";
                orderSheet.Cells[1, 12].Value = "Preparation Status";
                orderSheet.Cells[1, 13].Value = "Username";
                orderSheet.Cells[1, 14].Value = "Store";
                for (int r = 0; r < Feedbacks.Count(); r++)
                {
                    //orderSheet.Cells[r + 2, 1].Value = Feedbacks.ToList()[r].OrderStatus;
                    //orderSheet.Cells[r + 2, 2].Value = Feedbacks.ToList()[r].OrderId;
                    //orderSheet.Cells[r + 2, 3].Value = Feedbacks.ToList()[r].PaymentStatus;
                    //orderSheet.Cells[r + 2, 4].Value = Feedbacks.ToList()[r].PaymentMethod;
                    //orderSheet.Cells[r + 2, 5].Style.Numberformat.Format = "yyyy-MM-dd HH:mm:ss";
                    //orderSheet.Cells[r + 2, 5].Value = Feedbacks.ToList()[r].OrderDate;
                    //orderSheet.Cells[r + 2, 6].Style.Numberformat.Format = "R0.00";
                    //orderSheet.Cells[r + 2, 6].Value = Feedbacks.ToList()[r].Paid;
                    //orderSheet.Cells[r + 2, 7].Style.Numberformat.Format = "R0.00";
                    //orderSheet.Cells[r + 2, 7].Value = Feedbacks.ToList()[r].Change;
                    //orderSheet.Cells[r + 2, 8].Style.Numberformat.Format = "R0.00";
                    //orderSheet.Cells[r + 2, 8].Value = Feedbacks.ToList()[r].OrderDiscount;
                    //orderSheet.Cells[r + 2, 8].Style.Numberformat.Format = "R0.00";
                    //orderSheet.Cells[r + 2, 9].Value = Feedbacks.ToList()[r].OrderTotal;
                    //orderSheet.Cells[r + 2, 9].Style.Numberformat.Format = "R0.00";
                    //orderSheet.Cells[r + 2, 10].Value = Feedbacks.ToList()[r].VatTotal;
                    //orderSheet.Cells[r + 2, 10].Style.Numberformat.Format = "R0.00";
                    //orderSheet.Cells[r + 2, 11].Value = Feedbacks.ToList()[r].GrandTotal;
                    //orderSheet.Cells[r + 2, 12].Value = Feedbacks.ToList()[r].PreparationStatus;
                    //orderSheet.Cells[r + 2, 13].Value = Feedbacks.ToList()[r].Username;
                    //orderSheet.Cells[r + 2, 14].Value = Forms.FirstOrDefault(s => s.Store_ID == Feedbacks.ToList()[r].StoreId).StoreName;
                }

                package.SaveAs(fs);
                package.Dispose();
            }
            using (var stream = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;
            return(File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName));
        }