/// <summary> /// Render a rdlc (SQL Server Reporting Services) /// </summary> /// <param name="reportPath">Absolute file path</param> /// <param name="exportReportType">Export file extension</param> /// <param name="parameters">Report parameters</param> /// <param name="dataSources">Report datasources</param> /// <returns>Array file contents</returns> public byte[] RenderRdlc(string reportPath, ExportReportType exportReportType, Dictionary <string, string> parameters = null, Dictionary <string, object> dataSources = null) { try { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var report = new LocalReport(reportPath); if (dataSources != default) { foreach (var dataSource in dataSources) { report.AddDataSource(dataSource.Key, dataSource.Value); } } var result = report.Execute((RenderType)exportReportType, 1, parameters); return(result.MainStream); } catch (Exception e) { _logger?.Error(e, e.Message); throw; } }
public void TestReportNotFound() { var service = new ReportRenderer(null); const string path = "random path.rdlc"; const ExportReportType extension = ExportReportType.Pdf; Assert.Throws <FileNotFoundException>(() => { service.RenderRdlc(path, extension); }); }
public void TestReport() { var service = new ReportRenderer(null); var path = $"{Directory.GetCurrentDirectory()}\\Reporting\\BillExample.rdlc"; const ExportReportType extension = ExportReportType.Pdf; var pathResult = $"{Directory.GetCurrentDirectory()}\\Reporting\\BillExample.pdf"; var billReportData = new BillReportData { IsUser = true, Number = "23", FiscalName = "tax", DateString = DateTime.Now.ToShortDateString(), Cif = "cif", Address = "address", TaxableString = "tax", IvaAmount = "iva", TotalString = "total" }; var parameters = new Dictionary <string, string> { { "IsUserParameter", billReportData.IsUser.ToString() }, { "NumberParameter", billReportData.Number }, { "DateParameter", billReportData.DateString }, { "FiscalNameParameter", billReportData.FiscalName }, { "CifParameter", billReportData.Cif }, { "AddressParameter", billReportData.Address ?? "-" }, { "TotalTaxableParameter", billReportData.TaxableString }, { "IvaPercentageParameter", billReportData.IvaAmount }, { "TotalAmountParameter", billReportData.TotalString } }; var dataSources = new Dictionary <string, object> { { "ConceptsDataSet", billReportData.Concepts } }; var bytes = service.RenderRdlc(path, extension, parameters, dataSources); Assert.NotNull(bytes); Assert.True(bytes.Length > 78_000); Assert.True(bytes.Length < 79_000); File.WriteAllBytes(pathResult, bytes); }