Export RDLC Report

To export an RDLC report with the Bold Reports® ASP.NET Core Report Writer, load the report from the local file system, set the processing mode to local, and bind a custom business object data collection. The following steps show how to export an RDLC report using the Report Writer.

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.

Bind data source in Web API controller

To bind a data source for an RDLC report, configure the Web API to pass a business object data collection to the Report Writer, as in the following steps:

  1. Create a class and methods that returns business object data collection. Use the following code in your application Web API Service.

    public class ProductList
    {
        public string ProductName { get; set; }
        public string OrderId { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }
        public string Ingredients { get; set; }
        public string ProductImage { get; set; }
    
        public static IList GetData()
        {
            List<ProductList> datas = new List<ProductList>();
            ProductList data = new ProductList()
            {
                ProductName = "Baked Chicken and Cheese",
                OrderId = "323B60",
                Price = 55,
                Category = "Non-Veg",
                Ingredients = "grilled chicken, corn and olives.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Delite",
                OrderId = "323B61",
                Price = 100,
                Category = "Non-Veg",
                Ingredients = "cheese, chicken chunks, onions & pineapple chunks.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Tikka",
                OrderId = "323B62",
                Price = 64,
                Category = "Non-Veg",
                Ingredients = "onions, grilled chicken, chicken salami & tomatoes.",
                ProductImage = ""
            };
            datas.Add(data);
            return datas;
        }
    }
  2. This tutorial uses the Product List.rdlc report, which you can download as the Product List.rdlc sample report. Copy and paste the sample RDLC reports into the wwwroot/Resources folder. For more information, see Samples and demos.

  3. Create a folder Resources into the wwwroot folder in your application. Copy and paste the sample RDLC reports into the Resoures folder.

  4. 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)
        {
            // Here, we have loaded the Product List.rdlc sample report from application the Resources folder.
            FileStream inputStream = new FileStream(_hostingEnvironment.WebRootPath + @"\Resources\Product List.rdlc", FileMode.Open, FileAccess.Read);
            MemoryStream reportStream = new MemoryStream();
            inputStream.CopyTo(reportStream);
            reportStream.Position = 0;
            inputStream.Close();
            ......
        }
    }
  5. Initialize the Report Writer instance with the created reportStream, and set the ReportProcessingMode property to ProcessingMode.Local to provide the dataset collection for the RDLC report.

    BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
    writer.ReportProcessingMode = ProcessingMode.Local;
  6. Bind the business object data values collection by adding a new item to the DataSources as in the following code snippet:

    //Pass the dataset collection for report
    writer.DataSources.Clear();
    writer.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() });

    Data source Name is case sensitive and it should be same as in the data source name in the report definition and the Value accepts IList, DataSet, and DataTable inputs.

  7. Use the Save method of the Report Writer to generate the export document from the report stream. The Save method returns the generated file as a stream.

    [HttpPost]
    public IActionResult Export(string writerFormat)
    {
       // Here, we have loaded the Product List.rdlc sample report from application the Resources folder.
       FileStream inputStream = new FileStream(_hostingEnvironment.WebRootPath + @"\Resources\Product List.rdlc", FileMode.Open, FileAccess.Read);
       MemoryStream reportStream = new MemoryStream();
       inputStream.CopyTo(reportStream);
       reportStream.Position = 0;
       inputStream.Close();
       BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
       writer.ReportProcessingMode = ProcessingMode.Local;
    
       // Pass the dataset collection for report
       writer.DataSources.Clear();
       writer.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() });
    
       string fileName = null;
       WriterFormat format;
       string type = null;
    
       if (writerFormat == "PDF")
       {
           fileName = "Product List.pdf";
           type = "pdf";
           format = WriterFormat.PDF;
       }
       else if (writerFormat == "Word")
       {
           fileName = "Product List.docx";
           type = "docx";
           format = WriterFormat.Word;
       }
       else if (writerFormat == "CSV")
       {
           fileName = "Product List.csv";
           type = "csv";
           format = WriterFormat.CSV;
       }
       else
       {
           fileName = "Product List.xlsx";
           type = "xlsx";
           format = WriterFormat.Excel;
       }
    
       writer.LoadReport(reportStream);
       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;
    }
  8. The Report Writer now exports the RDLC report in your application.