public async Task <IActionResult> OnGetToday() { var bookings = await _bookingDataService.GetBookingsInbetweenDates(DateTime.Today, DateTime.Today); var json = new JsonResult(bookings); return(json); }
public async Task <ActionResult> OnPostExportExcel(DateTime Start, DateTime End) { BookingsListViewModel.Bookings = new List <BookingViewModel>(); BookingsListViewModel = await bookingDataService.GetBookingsInbetweenDates(Start, End); var from = DateTime.Now; var endDate = DateTime.Now.Date.ToShortDateString(); foreach (var booking in BookingsListViewModel.Bookings) { if (booking.BookingTime < from) { from = booking.BookingTime; } } var fromDateString = from.ToShortDateString(); System.IO.Stream spreadsheetStream = new System.IO.MemoryStream(); XLWorkbook workbook = new XLWorkbook(); IXLWorksheet worksheet = workbook.Worksheets.Add("Orders"); worksheet.Cell(1, 1).SetValue("KUNNR"); worksheet.Cell(1, 2).SetValue("DATO"); worksheet.Cell(1, 3).SetValue("ORDNR"); worksheet.Cell(1, 4).SetValue("PALLER"); worksheet.Cell(1, 5).SetValue("TRANSPORTØR"); worksheet.Cell(1, 6).SetValue("KONTAKTOPLYSNINGER"); worksheet.Cell(1, 7).SetValue("BOOKETTID"); worksheet.Cell(1, 8).SetValue("PORT"); worksheet.Cell(1, 9).SetValue("KUNDE"); worksheet.Cell(1, 10).SetValue("FAKTISK_\nANKOMST"); worksheet.Cell(1, 11).SetValue("START_\nLÆSNING"); worksheet.Cell(1, 12).SetValue("SLUT_\nLÆSNING"); var cellX = 1; var cellY = 2; foreach (var booking in BookingsListViewModel.Bookings) { foreach (var order in booking.OrdersListViewModel) { worksheet.Cell(cellY, cellX++).SetValue(order.CustomerNumber); worksheet.Cell(cellY, cellX++).SetValue(booking.BookingTime.ToShortDateString()); worksheet.Cell(cellY, cellX++).SetValue(order.OrderNumber); worksheet.Cell(cellY, cellX++).SetValue(order.TotalPallets); worksheet.Cell(cellY, cellX++).SetValue(booking.TransporterName); worksheet.Cell(cellY, cellX++).SetValue(booking.Email); worksheet.Cell(cellY, cellX++).SetValue(booking.BookingTime.ToShortTimeString()); worksheet.Cell(cellY, cellX++).SetValue(booking.Port); worksheet.Cell(cellY, cellX++).SetValue(order.SupplierViewModel.Name); worksheet.Cell(cellY, cellX++).SetValue(booking.ActualArrival.ToShortTimeString()); worksheet.Cell(cellY, cellX++).SetValue(booking.StartLoading.ToShortTimeString()); worksheet.Cell(cellY, cellX++).SetValue(booking.EndLoading.ToShortTimeString()); cellY++; cellX = 1; } } //Fixed column width - comes out messy if not fixed worksheet.ColumnWidth = 20; workbook.SaveAs(spreadsheetStream); spreadsheetStream.Position = 0; var fileName = "Ordre_FRA_" + fromDateString + "_TIL_" + endDate; return(new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = fileName + ".xlsx" }); }