public IHttpActionResult GetComplainReport(DateTime fromDate, DateTime toDate) { try { List <ComplainReportViewModel> complainList = new List <ComplainReportViewModel>(); IEnumerable <Complain> complains = new List <Complain>(); complains = _complain.GetAllComplains(fromDate, toDate); if (complains.Count() > 0) { foreach (Complain complain in complains) { if (complain.DriverId != null) { foreach (ComplainCategory category in complain.ComplainCategories) { if (category.IsSelected == true) { ComplainReportViewModel complainView = new ComplainReportViewModel(); complainView.driverName = _member.GetMember(complain.DriverId.Value).FullName; complainView.ntcNo = _member.GetMember(complain.DriverId.Value).NTCNo; complainView.complain = String.IsNullOrEmpty(category.Category.Description) ? String.Empty : category.Category.Description; complainView.complainStatus = complain.ComplainStatus; complainList.Add(complainView); } } } if (complain.ConductorId != null) { foreach (ComplainCategory category in complain.ComplainCategories) { if (category.IsSelected == true) { ComplainReportViewModel complainView = new ComplainReportViewModel(); complainView.driverName = _member.GetMember(complain.ConductorId.Value).FullName; complainView.ntcNo = _member.GetMember(complain.ConductorId.Value).NTCNo; complainView.complain = String.IsNullOrEmpty(category.Category.Description) ? String.Empty : category.Category.Description; complainView.complainStatus = complain.ComplainStatus; complainList.Add(complainView); } } } } } var messageData = new { code = Constant.SuccessMessageCode, message = Constant.MessageSuccess }; var returnObject = new { complainList = complainList, messageCode = messageData }; return(Ok(returnObject)); } catch (Exception ex) { string errorLogId = _eventLog.WriteLogs(User.Identity.Name, ex, MethodBase.GetCurrentMethod().Name); var messageData = new { code = Constant.ErrorMessageCode, message = String.Format(Constant.MessageTaskmateError, errorLogId) }; var returnObject = new { messageCode = messageData }; return(Ok(returnObject)); } }
public HttpResponseMessage ComplainManagementReport(DateTime fromDate, DateTime toDate) { try { List <ComplainReportViewModel> complainList = new List <ComplainReportViewModel>(); IEnumerable <Complain> complains = new List <Complain>(); complains = _complain.GetAllComplains(fromDate, toDate); if (complains.Count() > 0) { foreach (Complain complain in complains) { if (complain.DriverId != null) { foreach (ComplainCategory category in complain.ComplainCategories) { if (category.IsSelected) { ComplainReportViewModel complainView = new ComplainReportViewModel(); complainView.driverName = _member.GetMember(complain.DriverId.Value).FullName; complainView.ntcNo = _member.GetMember(complain.DriverId.Value).NTCNo; complainView.complain = String.IsNullOrEmpty(category.Category.Description) ? String.Empty : category.Category.Description; complainView.complainStatus = complain.ComplainStatus; complainList.Add(complainView); } } } if (complain.ConductorId != null) { foreach (ComplainCategory category in complain.ComplainCategories) { if (category.IsSelected) { ComplainReportViewModel complainView = new ComplainReportViewModel(); complainView.driverName = _member.GetMember(complain.ConductorId.Value).FullName; complainView.ntcNo = _member.GetMember(complain.ConductorId.Value).NTCNo; complainView.complain = String.IsNullOrEmpty(category.Category.Description) ? String.Empty : category.Category.Description; complainView.complainStatus = complain.ComplainStatus; complainList.Add(complainView); } } } } } MemoryStream stream = new MemoryStream(); HttpResponseMessage result = new HttpResponseMessage(); using (var package = new ExcelPackage(stream)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Report - " + DateTime.Now.ToShortDateString()); // Add some formatting to the worksheet worksheet.TabColor = System.Drawing.Color.Blue; worksheet.DefaultRowHeight = 12; worksheet.HeaderFooter.FirstFooter.LeftAlignedText = string.Format("Generated: {0}", DateTime.Now.ToShortDateString()); worksheet.Row(1).Height = 20; worksheet.Row(2).Height = 18; // Start adding the header _exel.CreateExcelHeader(worksheet, "Complain Management Report", 1, 2, "A1", "H1", false, true); _exel.CreateExcelHeader(worksheet, "NTC NO", 3, 1, "A4", "B4", true, true); _exel.CreateExcelHeader(worksheet, "Driver Name", 3, 2, "C4", "D4", false, true); _exel.CreateExcelHeader(worksheet, "Complain", 3, 3, "E4", "F4", false, true); _exel.CreateExcelHeader(worksheet, "Status", 3, 4, "G4", "H4", false, true); int rowId = 5; int receiptCount = complainList.Count(); foreach (var s in complainList) { _exel.CreateExcelContents(worksheet, s.ntcNo, rowId, 1, "A" + rowId, "B" + rowId, true, true, false, false, "", ""); _exel.CreateExcelContents(worksheet, s.driverName, rowId, 2, "C" + rowId, "D" + rowId, true, true, false, false, "", ""); _exel.CreateExcelContents(worksheet, s.complain, rowId, 3, "E" + rowId, "F" + rowId, true, true, false, false, "", ""); _exel.CreateExcelContents(worksheet, s.complainStatus, rowId, 4, "G" + rowId, "H" + rowId, true, true, false, false, "", ""); rowId += 1; } package.Workbook.Properties.Title = "COMPLAIN MANAGEMENT REPORT"; package.Workbook.Properties.Author = "NTC"; package.Workbook.Properties.Company = "NTC"; result.Content = new ByteArrayContent(package.GetAsByteArray()); } result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Complain Management Report - " + DateTime.Now.ToShortDateString() + ".xlsx" }; return(result); } catch (Exception ex) { return(null); } }