Search results
PDF

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 Core 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 into the wwwroot 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
    {
        // 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();
            ......
        }
    }
  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 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;
        }
    
  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 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() });
    }
}
Contents
Having trouble getting help?Contact Support
Contents
Having trouble getting help?Contact Support