Export Subreport
To export a main report with subreports using the Bold Reports® ASP.NET MVC 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 MVC Report Writer application. If you do not have one, create it by following the ASP.NET MVC 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
Resourceson theApp_Datafolder 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 { [HttpPost] public ActionResult Export(string writerFormat) { // Here, we have loaded the sample reports from application the App_Data/Resources folder. FileStream mainReportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Resources\Side_By_SideMainReport.rdl"), FileMode.Open, FileAccess.Read); FileStream subReportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Resources\Side_By_SideSubReport.rdl"), FileMode.Open, FileAccess.Read); ...... } } -
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 ActionResult Export(string writerFormat) { FileStream mainReportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Resources\Side_By_SideMainReport.rdl"), FileMode.Open, FileAccess.Read); FileStream subReportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Resources\Side_By_SideSubReport.rdl"), FileMode.Open, FileAccess.Read); 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 ActionResult Pdf()
{
// Here, we have loaded the sample report from application the folder App_Data.
FileStream mainReportStream= new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Reports\MainReport.rdlc"), FileMode.Open, FileAccess.Read);
FileStream subreport1Stream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Reports\SubReport1.rdlc"), FileMode.Open, FileAccess.Read);
FileStream subreport2Stream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"\App_Data\Reports\SubReport2.rdlc"), FileMode.Open, FileAccess.Read);
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() });
}
}