[ASP NET MVC] using ReportViewer execute client report definition files (.rdlc) output report

Use ReportViewer execute client report definition files (.rdlc) to output reports


Foreword

In the past when using ASP.NET WebForm Web site development, I will face a multi-output reports using ReportViewer to, and with a client report definition files (.rdlc) to design the appearance of the report, it is actually quite flexible solution; now use ASP.NET MVC development, although View can not add any WebForm Control, but we can still build a shared WebForm page, on this page to join the familiar ReportViewer to assist output reports. For more details, please refer to the following article in practice.

Practice Description

The whole concept is to create a practice WebForm page in ASP.NET MVC project, using the page to the WebForm ReportViewer practice work-related output report; final report when there is demand for their output, just pass the ReportViewer in the Controller Information required to WebForm page, by using the WebForm page to render corresponding to the contents of the report. To practice the following description.

Establish ReportViewer Web Form as the report page

First, the new file ReportViewer.aspx

image

ScriptManager and join in WebForm ReportViewer form.

image



<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>






    


    
  
  

Because I want to share ReportViewer page, thus establishing ReportWarpper class to encapsulate all the information required for the ReportViewer, ultimately we can operate a plurality of information to the Controller to output different report forms. The package contains information rdlc file locations, data sources (ReportDataSource, ReportParameter), and whether to download a report control flag (IsDownloadDirectly).


{
    // Constructors
    public ReportWrapper()
    {
        ReportDataSources = new List
  
  
   
   ();
        ReportParameters = new List
   
   
    
    ();
    }


    // Properties
    public string ReportPath { get; set; }

    public List
    
    
     
      ReportDataSources { get; set; }

    public List
     
     
      
       ReportParameters { get; set; }

    public bool IsDownloadDirectly { get; set; }

}
     
     
    
    
   
   
  
  

Since the Request will come from behind Controller, and then output according to the contents of the report to obtain the relevant data into ReportWarpper, the sub-site to ReportViewer WebForm page report presents information based on ReportWarpper; because there are messaging needs across the page, so this use Session as passed ReportWarpper media. Next ReportWarpper based on the information provided can be practiced ReportViewer report output common logic, in fact, mainly set client report definition file (.rdlc) position, and given the data source information (ReportDataSource, ReportParameter), and finally determines whether or not the direct output files for users to download.


{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GenerateReport();
        }
    }

    private void GenerateReport()
    {

        var ReportWrapperSessionKey = "ReportWrapper";
        var rw = (ReportWrapper)Session[ReportWrapperSessionKey];
        if (rw != null)
        {
            // Rdlc location
            RptViewer.LocalReport.ReportPath = rw.ReportPath;

            // Set report data source
            RptViewer.LocalReport.DataSources.Clear();
            foreach (var reportDataSource in rw.ReportDataSources)
            { RptViewer.LocalReport.DataSources.Add(reportDataSource); }

            // Set report parameters
            RptViewer.LocalReport.SetParameters(rw.ReportParameters);

            // Refresh report
            RptViewer.LocalReport.Refresh();

            // Download report directly
            if (rw.IsDownloadDirectly)
            {
                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string extension;

                byte[] bytes = RptViewer.LocalReport.Render(
                   "Excel", null, out mimeType, out encoding, out extension,
                   out streamids, out warnings);

                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=sample.xls");
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.OutputStream.Write(bytes, 0, bytes.Length); 
            }

            // Remove session
           Session[ReportWrapperSessionKey] = null;
        }
        
    }
}

Build client report definition files (.rdlc)

rdlc as the core design of the report, according to necessity to report the appearance of design and data presentation. As a simple example, demand this year is the list of new employees list in the report, the final report output screen below, the yellow part of the transaction value, that is, to fill in the data report.

image

First, the addition of rdlc file named UserRpt

image

Set up a program targeting the following, simple to render the appearance of the report using the text box and data tables.

image

Form data set sources

In this step you can think of it as in the ViewModel design statements, and often use the data type are DataSet and Parameter, so we can use different reporting patterns according to different data sources report data patterns, respectively, the following to practice.

ReportDataSource - DataSet

The following is the obvious yellow block Table section, so you can use the DataSet as a data source of the report.

image

Firstly dataset file (.xsd) and establishing a report corresponding to the User Table fields are as follows.

image   image

Then it may be added just defined set of data in the rdlc

image

In this he added UserRptDataSet in User form as a report dataset. Remember to take better name recognition for this data set because the follow-up when setting the Report Viewer will be used to, so I will use DataSetName_TableName as the name for identification.

image

Finally, focus on the fields to bind data to the report.

image

Report Parameter

The following is a single yellow block due to the information, and therefore prefer to use parameters (Parameter) for the report's data source.

image

Was added directly to the parameter data rdlc

image

RptMakerParam establish a name for the report parameter. Because the follow-up when setting the Report Viewer to use, remember to take this parameter data relatively good name recognition, or if more than one parameter can be very confusing.

image

Then bind parameter data to the report

image

Output Report

Finally, write a test page, the report will be displayed when clicking browse through ReportViewer report, and click to download the report when the report will not be shown on the screen, but directly download the report as an Excel file.

image

controller code browsing the report are as follows


{
    public ActionResult BrowseReport()
    {
        // Prepare data in report
        UserRptDataSet ds = new UserRptDataSet();
        ds.User.AddUserRow("chris",  "chris chen", "Taipei");
        ds.User.AddUserRow("eunice", "eunice chen", "New Taipei");

        // Set report info
        ReportWrapper rw = new ReportWrapper();
        rw.ReportPath = Server.MapPath("/Report/Rdlc/UserRpt.rdlc");
        rw.ReportDataSources.Add(new ReportDataSource("UserRptDataSet_User", ds.User.Copy()));
        rw.ReportParameters.Add(new ReportParameter("RptMakerParam", "CHRIS"));
        rw.IsDownloadDirectly = false;

        // Pass report info via session
        Session["ReportWrapper"] = rw;

        // Go report viewer page
        return Redirect("/Report/ReportViewer.aspx");  
    }
}

After clicking the report to be displayed directly on the page.

image

Codes controller follows a report, and the difference flag is set to true IsDownloadDirectly

image

After clicking the link to download the report directly saved as an Excel file download

image

Reference Information

http://www.c-sharpcorner.com/UploadFile/ff2f08/incorporating-Asp-Net-mvc-and-sql-server-reporting-services/

http://pinnynet.blogspot.tw/2009/11/cliend-side-report.html


I hope this article can help to people in need

If the content is incorrect or there are other suggestions, please feel free to leave a message to the author Oh!

Original: Large column  [ASP NET MVC] using ReportViewer execution (.rdlc) Output Reports client report definition files


Guess you like

Origin www.cnblogs.com/petewell/p/11516473.html