public ActionResult CreateReport([FromBody] Model1Response modelResult) { try { var _reportService = new Services.ReportService(); var returnString = _reportService.GenerateReportAsync(modelResult); ActionResult document = File(returnString, System.Net.Mime.MediaTypeNames.Application.Octet, "Report Result Data.pdf"); DocumentResponseModel response = new DocumentResponseModel() { hasError = false, messageError = null, document = document }; return(Content(JsonConvert.SerializeObject(response), "application/json")); } catch (Exception ex) { DocumentResponseModel response = new DocumentResponseModel() { hasError = true, messageError = ex.Message, document = null }; return(Content(JsonConvert.SerializeObject(response), "application/json")); } }
public byte[] GenerateReportAsync(Model1Response modelResult) { DataSet ds = new DataSet(); ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.objToOptimize.cropsSelected))); ReportDataSource rds1 = new ReportDataSource { Name = "CropsToOptimizeDataSet", Value = ds.Tables[0] }; ds = new DataSet(); ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.listPlantResult))); ReportDataSource rds2 = new ReportDataSource { Name = "PlantDataSet", Value = ds.Tables[0] }; ds = new DataSet(); ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.listHarvestResult))); ReportDataSource rds3 = new ReportDataSource { Name = "HarvestDataSet", Value = ds.Tables[0] }; ds = new DataSet(); ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.listLaborResult.listLaborPerWeeks))); ReportDataSource rds4 = new ReportDataSource { Name = "LaborDataSet2", Value = ds.Tables[0] }; ds = new DataSet(); ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.listLaborResult.listLaborRequirementSummary))); ReportDataSource rds5 = new ReportDataSource { Name = "LaborDataSet", Value = ds.Tables[0] }; ds = new DataSet(); ds.Tables.Add(JsonConvert.DeserializeObject <DataTable>(JsonConvert.SerializeObject(modelResult.incomeCostResult.listIncomeCostResultByCrops))); ReportDataSource rds6 = new ReportDataSource { Name = "IncomesDataSet", Value = ds.Tables[0] }; List <string> cropsSelected = new List <string>(); foreach (CropsConfigurationModel crop in modelResult.objToOptimize.cropsSelected) { cropsSelected.Add(crop.crop); } string locationSelected = modelResult.objToOptimize.locationSelected.location; ReportParameter[] parametros = { new ReportParameter("locationSelected", locationSelected), new ReportParameter("cropsSelected", String.Join(", ", cropsSelected)), new ReportParameter("incomes", modelResult.incomeCostResult.incomes.ToString()), new ReportParameter("plantingCost", modelResult.incomeCostResult.plantCost.ToString()), new ReportParameter("laborCost", modelResult.incomeCostResult.laborCost.ToString()) }; LocalReport lr = new LocalReport(); var Reportpath = @".\Reports\ReportResultData.rdlc"; lr.ReportPath = Reportpath; lr.DisplayName = "Report Result Data.pdf"; lr.SetParameters(parametros); lr.DataSources.Add(rds1); lr.DataSources.Add(rds2); lr.DataSources.Add(rds3); lr.DataSources.Add(rds4); lr.DataSources.Add(rds5); lr.DataSources.Add(rds6); var reportType = "pdf"; string mimeType; string encoding; string fileNameExtension; Warning[] warnings; string[] streams; var renderedBytes = lr.Render(reportType, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); return(renderedBytes); }