Export Subreport
To export a main report with subreports using the Bold Reports® ASP.NET Core Report Writer, load each subreport with the LoadSubreport() method before you load the main report. The Report Writer exports the report to popular file formats such as PDF, Microsoft Word, and Microsoft Excel without previewing it in a webpage.
You need an ASP.NET Core Report Writer application. If you do not have one, create it by following the ASP.NET Core Report Writer getting started guide.
How do I export an RDL subreport?
To export an RDL subreport with the Report Writer, pass the subreport name and stream to LoadSubreport(), load the main report with LoadReport(), and call the Save method.
This tutorial uses the
Side_By_SideMainReport.rdlandSide_By_SideSubReport.rdlreports, which you can download as the subreport sample reports. You can get the report from Bold Reports® installation location. The reports used from installed location requiresNorthwindIO_Reports.sdfdatabase to run, so add the database to your application. For more information, refer to Samples and demos.
Follow these steps to export the RDL subreport in the specified format.
-
Create a folder
Resourcesinto thewwwrootfolder in your application. Copy and paste the sample RDL reports into theResouresfolder. -
Open the
HomeController.csfile in your application and add theExport()function to load the report as stream. Refer to the following code snippet:public class HomeController : Controller { // IWebHostEnvironment used with sample to get the application data from wwwroot. private Microsoft.AspNetCore.Hosting.IWebHostEnvironment _hostingEnvironment; // IWebHostEnvironment initialized with controller to get the data from application data folder. public HomeController(Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } [HttpPost] public IActionResult Export(string writerFormat) { string basePath = _hostingEnvironment.WebRootPath; // Here, we have loaded the sample reports from application the wwwroot\Resources folder. FileStream inputMainStream = new FileStream(basePath + @"\Resources\Side_By_SideMainReport.rdl", FileMode.Open, FileAccess.Read); MemoryStream mainReportStream = new MemoryStream(); inputMainStream.CopyTo(mainReportStream); mainReportStream.Position = 0; inputMainStream.Close(); FileStream inputSubStream = new FileStream(basePath + @"\Resources\Side_By_SideSubReport.rdl", FileMode.Open, FileAccess.Read); MemoryStream subReportStream = new MemoryStream(); inputSubStream.CopyTo(subReportStream); subReportStream.Position = 0; inputSubStream.Close(); ...... } } -
Initialize the Report Writer instance and pass the subreport name and stream to the
LoadSubreport()method in Report Writer instance.BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(); writer.LoadSubreport("Side_By_SideSubReport", subReportStream); -
Load the main report stream with the Report Writer instance, as in the following code snippet:
writer.LoadReport(mainReportStream); -
Use the
Savemethod of the Report Writer to generate the export document from the report stream. TheSavemethod returns the generated file as a stream.[HttpPost] public IActionResult Export(string writerFormat) { string basePath = _hostingEnvironment.WebRootPath; // Here, we have loaded the sample reports from application the wwwroot\Resources folder. FileStream inputMainStream = new FileStream(basePath + @"\Resources\Side_By_SideMainReport.rdl", FileMode.Open, FileAccess.Read); MemoryStream mainReportStream = new MemoryStream(); inputMainStream.CopyTo(mainReportStream); mainReportStream.Position = 0; inputMainStream.Close(); FileStream inputSubStream = new FileStream(basePath + @"\Resources\Side_By_SideSubReport.rdl", FileMode.Open, FileAccess.Read); MemoryStream subReportStream = new MemoryStream(); inputSubStream.CopyTo(subReportStream); subReportStream.Position = 0; inputSubStream.Close(); BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter(); writer.LoadSubreport("Side_By_SideSubReport", subReportStream); writer.LoadReport(mainReportStream); string fileName = null; WriterFormat format; string type = null; if (writerFormat == "PDF") { fileName = "Side_By_SideMainReport.pdf"; type = "pdf"; format = WriterFormat.PDF; } else if (writerFormat == "Word") { fileName = "Side_By_SideMainReport.docx"; type = "docx"; format = WriterFormat.Word; } else if (writerFormat == "CSV") { fileName = "Side_By_SideMainReport.csv"; type = "csv"; format = WriterFormat.CSV; } else { fileName = "Side_By_SideMainReport.xlsx"; type = "xlsx"; format = WriterFormat.Excel; } MemoryStream memoryStream = new MemoryStream(); writer.Save(memoryStream, format); // Download the generated export document to the client side. memoryStream.Position = 0; FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/" + type); fileStreamResult.FileDownloadName = fileName; return fileStreamResult; } -
The Report Writer now exports the RDL report with its subreport in your application.
How do I export an RDLC subreport?
To export an RDLC report with subreports, load the subreport streams into the Report Writer before you load the main report, so that the subreport processing event works. The following code example shows the order.
[HttpPost]
public IActionResult Pdf()
{
string basePath = _hostingEnvironment.WebRootPath;
// Here, we have loaded the sample report from application the folder wwwroot.
FileStream inputMainStream = new FileStream(basePath + @"\Reports\MainReport.rdlc", FileMode.Open, FileAccess.Read);
MemoryStream mainReportStream = new MemoryStream();
inputMainStream.CopyTo(mainReportStream);
mainReportStream.Position = 0;
inputMainStream.Close();
FileStream inputsub1Stream = new FileStream(basePath + @"\Reports\SubReport1.rdlc", FileMode.Open, FileAccess.Read);
MemoryStream subreport1Stream = new MemoryStream();
inputsub1Stream.CopyTo(subreport1Stream);
subreport1Stream.Position = 0;
inputsub1Stream.Close();
FileStream inputsub2Stream = new FileStream(basePath + @"\Reports\SubReport2.rdlc", FileMode.Open, FileAccess.Read);
MemoryStream subreport2Stream = new MemoryStream();
inputsub2Stream.CopyTo(subreport2Stream);
subreport2Stream.Position = 0;
inputsub2Stream.Close();
ReportWriter writer = new ReportWriter();
writer.ReportProcessingMode = ProcessingMode.Local;
writer.SubreportProcessing += ReportWriter_SubreportProcessing;
//Adding sub report stream going to be used with exporting report.
writer.LoadSubreport("SubReport1", subreport1Stream);
writer.LoadSubreport("SubReport2", subreport2Stream);
//Loading the report going to export as PDF.
writer.LoadReport(mainReportStream);
writer.DataSources.Clear();
writer.DataSources.Add(new ReportDataSource { Name = "DataSet", Value = MainReport.GetData() });
// Steps to generate PDF report using Report Writer.
MemoryStream memoryStream = new MemoryStream();
writer.Save(memoryStream, BoldReports.Writer.WriterFormat.PDF);
// Download the generated from client.
memoryStream.Position = 0;
FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");
fileStreamResult.FileDownloadName = "Invoice.pdf";
return fileStreamResult;
}
private void ReportWriter_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
if (e.ReportPath == "SubReport1")
{
//Pass the dataset collection for subreport
e.DataSources.Clear();
e.DataSources.Add(new ReportDataSource { Name = "DataSet1", Value = SubReport1.GetData() });
}
else if (e.ReportPath == "SubReport2")
{
//Pass the dataset collection for subreport
e.DataSources.Clear();
e.DataSources.Add(new ReportDataSource { Name = "DataSet2", Value = SubReport2.GetData() });
}
}