Export Subreport

To export a main report with subreports using the Bold Reports® ASP.NET Web Forms 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 Web Forms Report Writer application. If you do not have one, create it by following the ASP.NET Web Forms 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 stream with the Report Writer instance, and call the Save method.

In this tutorial, the Side_By_SideMainReport.rdl, Side_By_SideSubReport.rdl reports are used, and you can download the subreport sample reports. You can get the report from Bold Reports® installation location. The reports used from installed location requires NorthwindIO_Reports.sdf database to run, so add the database to your application. For more information, refer to Samples and demos.

Refer to the following steps to export the RDL sub report with specified format.

  1. Create a folder Resources in your application. Copy and paste the sample RDL reports into the Resources folder.

  2. Open the Default.aspx.cs file in your application and add the Export()function to load the report as stream. Refer to the following code snippet:

    public partial class _Default : Page
    {
        protected void ExportButton_Click(object sender, EventArgs e)
        {
            // Here, we have loaded the sample reports from application the Resources folder.
            FileStream mainReportStream = new FileStream(Server.MapPath("~/Resources/Side_By_SideMainReport.rdl"), FileMode.Open, FileAccess.Read);
            FileStream subReportStream = new FileStream(Server.MapPath("~/Resources/Side_By_SideSubReport.rdl"), FileMode.Open, FileAccess.Read);
        }
    }
  3. 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);
  4. After loading the main report stream using Report Writer instance. Refer to the following code snippet:

    writer.LoadReport(mainReportStream);
  5. You can use the Save method in Report Writer to generate the export document along with information of the report stream, it will return the generated file as Stream.

    protected void ExportButton_Click(object sender, EventArgs e)
    {
        FileStream mainReportStream = new FileStream(Server.MapPath("~/Resources/Side_By_SideMainReport.rdl"), FileMode.Open, FileAccess.Read);
        FileStream subReportStream = new FileStream(Server.MapPath("~/Resources/Side_By_SideSubReport.rdl"), FileMode.Open, FileAccess.Read);
        BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
        HttpContext httpContext = System.Web.HttpContext.Current;
    
        writer.LoadSubreport("Side_By_SideSubReport", subReportStream);
        writer.LoadReport(mainReportStream);
        string fileName = null;
        WriterFormat format;
        string type = null;
    
        if (this.ExportFormat.SelectedValue == "PDF")
        {
            fileName = "Side_By_SideMainReport.pdf";
            type = "pdf";
            format = WriterFormat.PDF;
        }
        else if (this.ExportFormat.SelectedValue == "Word")
        {
            fileName = "Side_By_SideMainReport.docx";
            type = "docx";
            format = WriterFormat.Word;
        }
        else if (this.ExportFormat.SelectedValue == "CSV")
        {
            fileName = "Side_By_SideMainReport.csv";
            type = "csv";
            format = WriterFormat.CSV;
        }
        else
        {
            fileName = "Side_By_SideMainReport.xlsx";
            type = "xlsx";
            format = WriterFormat.Excel;
        }
        writer.Save(fileName, format, httpContext.Response);  
    }
  6. Now, the RDL report exported successfully 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.

protected void ExportButton_Click(object sender, EventArgs e)
{
    // Here, we have loaded the sample report from application the folder App_Data.
    FileStream mainReportStream = new FileStream(Server.MapPath(@"\Resources\MainReport.rdlc"), FileMode.Open, FileAccess.Read);
    FileStream subreport1Stream = new FileStream(Server.MapPath(@"\Resources\SubReport1.rdlc"), FileMode.Open, FileAccess.Read);
    FileStream subreport2Stream = new FileStream(Server.MapPath(@"\Resources\SubReport2.rdlc"), FileMode.Open, FileAccess.Read);
    ReportWriter writer = new ReportWriter();
    writer.ReportProcessingMode = ProcessingMode.Local;
    writer.SubreportProcessing += ReportWriter_SubreportProcessing;
    string fileName = null;
    WriterFormat format;

    //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() });
    HttpContext httpContext = System.Web.HttpContext.Current;
    writer.Save(fileName, format, httpContext.Response);
}

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() });
    }
}