How to generate PDF/ xls report from .rdlc without using Reporting services in c#.

In this blog, I am going to show how to create a Report dynamically using a DataSet in Report document (*.rdlc). and show the

report without much effort.


Step 1: Create a Report(.rldc) using SSRS or any other tool. Add appropriate  DataSet and DataSource and with required columns

create a table and Bind DataColumns. Once Report created build and Goto Code behind solution.

Step 2: Now in the Code behind, add the following codes and required references.

Note: I have added just 3 fields report and the same field should be used in code behind also with exact DataSet name as it is

in Report. Currently DataSet has hardcode value which you can replace with fetched value from Database.


using System.Collections.Generic;
using Microsoft.Reporting.WebForms;


        #region ReportGenerateFromRDLFile
        public static void RenderReport()
        {
            string sReportPath = string.Empty;
            string sExtension = ".xls";

            try
            {
                sReportPath = @"D:\Report_Folder\";

                string filedir = string.Concat(new object[] {
                                  sReportPath,Path.DirectorySeparatorChar,"Report_Name", Path.DirectorySeparatorChar });

                //Path where you want to generate Report
                if (!(string.IsNullOrEmpty(filedir) && Directory.Exists(filedir)))
                    Directory.CreateDirectory(filedir);

                string sFilename = filedir + "Report_Name" + sExtension;

                //Creating dummy DataTable for binding to report. Field and Parameter name must match
                System.Data.DataTable dtProduit = new System.Data.DataTable();
                dtProduit.Columns.Add("fullname", typeof(string));
                dtProduit.Columns.Add("telephone1", typeof(string));
                dtProduit.Columns.Add("emailaddress1", typeof(string));

                DataRow dr = dtProduit.NewRow();
                dr["fullname"] = "Aayush Singh";
                dr["telephone1"] = "9393287832748";
                dr["emailaddress1"] = "Venky@gmail.com";
                dtProduit.Rows.Add(dr);

                //Path where RDL file is stored
                string sRDL_File_Path = "D:\\Test_Report\\Report.rdl";
                Dictionary dicReportDatasources = new Dictionary();
                string sReportDownloadFormat = "EXCEL";
                string sFilePath = sFilename;
                byte[] byteresults;

                //DataSet name should be same as in RDLC report
                dicReportDatasources.Add("DataSet1", dtProduit);

                //Creating Report
                Microsoft.Reporting.WebForms.ReportParameter[] arrReportParameter = new Microsoft.Reporting.WebForms.ReportParameter[1];


                arrReportParameter[0] = new Microsoft.Reporting.WebForms.ReportParameter("Name", "Venky Dss");


                byteresults = GenerateReportUsingRDLC(
                    sRDL_File_Path,
                    dicReportDatasources,
                    sReportDownloadFormat,
                    arrReportParameter);

                //Step 5 Generating XLS/ PDF File
                if (byteresults != null)
                {
                    CreateFileUsingBytes(byteresults, sFilePath);
                }
            }
            catch (Exception e)
            {
            }
        }

        private static byte[] GenerateReportUsingRDLC(string sReportpath, Dictionary dicReportDatasources, string sReportDownloadFormat, ReportParameter[] arrReportParameter)
        {
            Byte[] result;

            using (LocalReport report = new LocalReport())
            {

                report.ReportPath = sReportpath;
                foreach (var datasource in dicReportDatasources)
                {
                    ReportDataSource rds = new ReportDataSource();
                    rds.Name = datasource.Key;
                    rds.Value = datasource.Value;
                    report.DataSources.Add(rds);

                    report.SetParameters(arrReportParameter);

                }

                dicReportDatasources.Clear();
                byte[] mybytes = report.Render(sReportDownloadFormat);
                result = mybytes;

            }
            return result;
        }

        private static void CreateFileUsingBytes(byte[] byteresults, string sFilePath)
        {
            using (FileStream stream = File.OpenWrite(sFilePath))
            {
                stream.Write(byteresults, 0, byteresults.Length);
            }
        }
        #endregion

Comments

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image