Search results
Suggest a FeaturePDF

Export Subreport

You can export a Main report with subreport with popular file formats such as PDF, Microsoft Word, and Microsoft Excel without previewing the report in webpage using ASP.NET Report Writer application.

This section requires an ASP.NET MVC Report Writer application, if you don’t have, then create by referring to the Getting-Started documentation.

Export RDL Subreport

In this tutorial, the Side_By_SideMainReport.rdl, Side_By_SideSubReport.rdl reports is used, and it can be downloaded here. 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 on App_Data folder in your application. Copy and paste the sample RDL reports into the Resoures folder.

  2. Open the HomeController.cs file in your application and add the Export()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);
            ......
        }
    }
  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.

        [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;
        }
    
  6. Now, the RDL report exported successfully in your application.

Export RDLC subreport

To export the RDLC report along with subreport, we need to load the subreport streams before loading a main report to the Report Writer as in the following code example, then only subreport processing event will work.

[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() });
    }
}
CONTENTS
Having trouble getting help?
Contact Support
CONTENTS
Having trouble getting help?
Contact Support