public void PopulateSupplierInfo(Supplier supplier, SupplierReportModel reportModel) { reportModel.SupplierName = supplier.Name; var addressLines = new string[5]; addressLines[0] = supplier.Address1.Trim(); addressLines[1] = supplier.Address2.Trim(); addressLines[2] = supplier.Address3.Trim(); addressLines[3] = supplier.Address4.Trim(); addressLines[4] = supplier.Address5.Trim(); var i = 0; while (i < addressLines.Length - 1 && String.IsNullOrEmpty(addressLines[i])) { i++; } reportModel.SupplierAddress1 = i < addressLines.Length ? addressLines[i++] : String.Empty; while (i < addressLines.Length - 1 && String.IsNullOrEmpty(addressLines[i])) { i++; } reportModel.SupplierAddress2 = i < addressLines.Length ? addressLines[i++] : String.Empty; while (i < addressLines.Length - 1 && String.IsNullOrEmpty(addressLines[i])) { i++; } reportModel.SupplierAddress3 = i < addressLines.Length ? addressLines[i++] : String.Empty; while (i < addressLines.Length - 1 && String.IsNullOrEmpty(addressLines[i])) { i++; } reportModel.SupplierAddress4 = i < addressLines.Length ? addressLines[i++] : String.Empty; while (i < addressLines.Length - 1 && String.IsNullOrEmpty(addressLines[i])) { i++; } reportModel.SupplierAddress5 = i < addressLines.Length ? addressLines[i++] : String.Empty; reportModel.SupplierTel = supplier.Telephone; reportModel.SupplierFax = supplier.Fax; }
public HttpResponseMessage Report() { try { string tempPath = ConfigurationManager.AppSettings["TempPath"]; long ticks = DateTime.Now.Ticks; string fileName = $"SuppliersReport-{ticks}.xls"; string fullName = $"{tempPath}\\" + fileName; List <SupplierViewModel> allAsync = service.GetAll(); string headerValue = "Supplier Report \n Printed " + DateTime.Now.ToString("dd-MM-yyyy"); List <SupplierReportModel> reportdata = allAsync.Select(x => new SupplierReportModel { Name = x.Name, Address = x.Address, Phone = x.Phone, Due = x.Due, Modified = x.Modified }).ToList(); SupplierReportModel item = new SupplierReportModel { Name = "Total", Due = reportdata.Sum(x => x.Due), Modified = DateTime.Now }; reportdata.Add(item); GenericReportGenerator <SupplierReportModel> .WriteExcel(reportdata, headerValue, fullName); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(fullName, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; return(result); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex)); } }