How do I dynamicaly create an SSRS Report?

这一生的挚爱 提交于 2019-12-02 05:23:58

You can create the SSRS report dynamically based on the data set returned by the stored procedure. The report format (RDL) is documented and its an XML format. So you can use System.XML namespace to generate RDL. Alternate (and unsupported) way is to refer RDL object model assembly (Microsoft.ReportingServices.RdlObjectModel) - you can locate the assembly on SSRS 2008 server machine and copy it on your local machine. It offers an object model to read/generate RDL.

I have adopted approach where RDL is generated (as XML) dynamically based on the data-table and then publish the RDL on SSRS server using web services API.

One solution might be to modify your SP so that the data returned looks something like:

ColNameA       DataA       ColNameB    DataB
AccountNumber, 1234567890, CustomerID, 948477586
AccountNumber, 5466584426, CustomerID, 458852244

Then, in SSRS drag out a table. Create a group on ColNameA. In that Group Row, place the Field ColNameA in the first Column, place ColNameB in the second column.

In the Details Row, place DataA in the first column, and DataB in the second column, and should look like this:

The sample query i used was:

select 'AccountNumber' as ColNameA, 1234567890 as DataA, 'CustomerID' as ColNameB, 0987654321 as DataB UNION 
select 'AccountNumber' as ColNameA, 5546488393 as DataA, 'CustomerID' as ColNameB, 4747599393 as DataB

Getting the names of the Columns (AccountNumber, CustomerID or CustomerName, CustomerAddress) will be the key. You can get them via:

select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'my_table_name'

You can use the Rdlobjectmodel provided by the Report designer to create and alter a report.

You can reference the Microsoft.ReportingServices.Designer.Controls in to your project but that includes all the dependencies as well which is 20+ assembly or you can copy the following set of DLLs in a separate folder at your project root level and use it to reference the DLLs

 Microsoft.ReportingServices.QueryDesigners
 Microsoft.ReportingServices.Designer.Controls
 Microsoft.ReportingServices.RdlObjectModel
 Microsoft.ReportingServices.ReportDesign.Common
 Microsoft.ReportingServices.RichText
 Microsoft.ReportingServices.RPLObjectModel


  private Report LoadReportTemplate()
  {
        const string docPath = "template.rdl";//path for your template report
        using (var fs = new FileStream(docPath, FileMode.Open))
        {
            var report = Report.Load(fs);
            return report;
        }
  }

Follow the link for Documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!