Getting Started with ASP.NET MVC Application

This section describes how to export the Report Server report as PDF, Word, Excel and HTML formats in ASP.NET MVC application using ReportWriter.

Project Creation

This section illustrates how to add ReportWriter to the ASP.NET MVC application. It includes the following steps.

  1. Create a new ASP.NET MVC project by selecting the WEB category from the listed project template in Microsoft Visual Studio IDE.

  2. The following screenshot displays how to select empty application template with razor view engine.

Create CSHTML Page

To create a new CSHTML view page in the application follow the below steps.

  1. Right click on the ReportWriter folder from Views followed by Add and then click View.

  2. Specify the name and then click on OK.

Add References

  1. In the Solution Explorer, Right-click the References folder and then click Add Reference.

  2. Add the following references

    • Syncfusion.Chart.Wpf
    • Syncfusion.Compression.Base
    • Syncfusion.DocIO.Base
    • Syncfusion.EJ.ReportViewer
    • Syncfusion.Gauge.Wpf
    • Syncfusion.Linq.Base
    • Syncfusion.Pdf.Base
    • Syncfusion.SfMaps.Wpf
    • Syncfusion.Shared.Wpf
    • Syncfusion.XlsIO.Base

    NOTE

    You can get the above assemblies from the installed location %localappdata%\Syncfusion\ReportsSDK\Samples\Common\Assemblies

Registering Assemblies within the Web.config file

In your application’s root web.config file, add the below assembly information within the <assemblies> tag.

  • html
  • <system.web>
        <compilation debug="true" targetFramework="4.5">
            <assemblies>
                <add assembly="Syncfusion.Chart.WPF, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.Compression.Base, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.DocIO.Base, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.EJ.ReportViewer, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.Gauge.WPF, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.Linq.Base, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.Pdf.Base, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.SfMaps.WPF, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.Shared.WPF, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
                <add assembly="Syncfusion.XlsIO.Base, Version=14.1400.0.42, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89"/>
            </assemblies>
        </compilation>      
    </system.web>

    Registering namespaces within Web.config

    Now you need to register the below mentioned two namespaces in the web.config file present within the Views folder as well as the Root directory of your application.

    • Syncfusion.EJ.ReportViewer
  • html
  • <namespaces>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>    
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Web.WebPages"/>    
        <add namespace="Syncfusion.EJ.ReportViewer"/>
    </namespaces>

    ReportWriter Initialization

    1. Initialize ReportWriter by using the controller in the Application. To create a controller in the Application, please follow the below steps.

    2. Specify the name to the controller and then click on OK.

    3. Set the following properties to ReportWriter.

      • ReportPath - Set the local file system or Report Server path of report.

      • ReportProcessingMode - Set ProcessingMode as Remote for RDL and Report Server report also Local for RDLC report.

      • WriterFormat - Set WriterFormat as PDF, Excel, Word and HTML.

    4. Add the following code to the ReportWriter in the controller class.

      public ActionResult Index(string writerFormat)
      {
          try
          {
              string fileName = null;
              WriterFormat format;
              HttpContext httpContext = System.Web.HttpContext.Current;
              ReportWriter reportWriter = new ReportWriter();
              reportWriter.ReportPath = Server.MapPath("~/App_Data/GroupingAgg.rdl");
              reportWriter.ReportProcessingMode = ProcessingMode.Remote;
      
              if (writerFormat == "PDF")
              {
                  fileName = "GroupingAgg.pdf";
                  format = WriterFormat.PDF;
              }
              else if (writerFormat == "Word")
              {
                  fileName = "GroupingAgg.doc";
                  format = WriterFormat.Word;
              }
              else if (writerFormat == "Html")
              {
                  fileName = "GroupingAgg.Html";
                  format = WriterFormat.HTML;
              }
              else
              {
                  fileName = "GroupingAgg.xls";
                  format = WriterFormat.Excel;
              }
              reportWriter.Save(fileName, format, httpContext.Response);
           }
           catch
           {
           }
           return View();
       }
    5. Add the following code example in the <body> tag of the CSHTML page to view ReportWriter export options.

      <body>
          <div class="container">
              <div class="header">
                  <span style="color: white; font-size: 2em">ReportWriter Demo</span>
              </div>
              <div class="content_section">
                  @{Html.BeginForm("Index", "ReportWriter", FormMethod.Post);
                  {
                      <div id="description_Pane" style="text-align: justify;">
                          <h3>Description</h3>
                          <span>
                              Essential Report Writer is a powerful control for exporting RDL files into specified
                              format files.The following are some of the key features of this component: Provides
                              support for various chart types that include Area, Bar, Column, Pie, Funnel, Radar, Line and
                              so on. Provides support for both Matrix and Table and also provides support for
                              both single, and multi-level row groupings and column groupings. Provides support
                              for other common controls such as Textbox, Image, Rectangle, Line and both Circular
                              and Linear Gauges. Provides support for report parameter.
                           </span>
                      </div>
                      <div id="export_Pane" style="margin-top: 4%;">
                          <h3>Export Report</h3>
                          <span>
                              Choose a file format to view the selected document generated from Report file by using Essential ReportWriter.
                          </span>
                          <div id="selection_Pane" style="margin-top: 2%;">
                              <span style="font-size:large;">
                                  <strong>Save As : </strong>
                              </span>
      
                              <input type="radio" name="writerFormat" value="PDF" checked="checked" />
                              <label for="btnPDf">PDF </label>
      
                              <input type="radio" name="writerFormat" value="Word" />
                              <label for="btnWord">Word </label>
      
                              <input type="radio" name="writerFormat" value="xls" />
                              <label for="btnxls">Excel </label>
      
                              <input type="radio" name="writerFormat" value="Html" />
                              <label for="btnHtml">HTML </label>
      
                              <input type="submit" name="button" value="Generate" style="width: 18%; margin-left: 2%;"/>
                          </div>
                      </div>
      
                      Html.EndForm();
                  }
                 }
              </div>
          </div>
       </body>
    6. Add the style to the ReportWriter by using the following code example in the <head> tag of the CSHTML page.

      <head>
          <meta name="viewport" content="width=device-width" />
          <title>Essential Studio Report Writer</title>
          <style>
              .container {
                  width: 72%;
                  border-color: rgb(39, 161, 174);
                  border-width: medium;
                  border-style: double;
                  position: absolute;
                  left: 14%;
                  height: 97.5%;
              }
      
              .header {
                  background-color: rgb(39, 161, 174);
                  text-align: center;
              }
      
              .content_section{
                  height:100%;
                  position: absolute;
                  top: 10%;
                  left: 3%;
                  right: 3%;
              }
          </style>
      </head>
    7. Run the application. The following output displays ReportWriter export options, Select the export option and click on Generate button.

    8. The following output displays exported report in the PDF format.

    Generate RDL Reports

    The ReportWriter has options to save the RDL reports. The following code example helps you to generate the RDL report using ReportWriter.

    Specify the ReportPath, ReportProcessingMode and WriterFormat properties for ReportWriter to generate report.

  • c#
  • string reportPath = @"..\ReportTemplate\GroupingAgg.rdl";
    ReportWriter reportWriter = new ReportWriter(reportPath);
    reportWriter.ReportProcessingMode = ProcessingMode.Remote;
    reportWriter.Save("GroupingAgg.xls", WriterFormat.Excel);

    Generate Report Server Reports

    The ReportWriter has options to save the Report Server reports. The following code example helps you to generate the Report Server report using ReportWriter.

    Specify the ReportPath, ReportServerUrl, ReportServerCredential, ReportProcessingMode and WriterFormat properties for ReportWriter to generate report.

  • c#
  • HttpContext httpContext = System.Web.HttpContext.Current;
    ReportWriter reportWriter = new ReportWriter();
    reportWriter.ReportPath = "45db67a0-3fd6-4684-b03c-aa640a521c97";
    reportWriter.ReportServerUrl = "http://reportserver.syncfusion.com:80/";
    reportWriter.ReportingServer = new ReportingServerExt();
    reportWriter.ReportServerCredential = new System.Net.NetworkCredential("guest", "demo");
    reportWriter.ReportProcessingMode = ProcessingMode.Remote;
    reportWriter.Save("GroupingAgg.doc", WriterFormat.Word, httpContext.Response);

    Report Server WebAPI Service

    Report Server implemented with necessary capabilities to access the contents through WebAPI service. In this tutorial, we are going to use the following API methods from Report Server WebAPI service.

    API Method Description
    download-report Download the report from Report Server using report id.
    download-data-source Download the report from Report Server using shared datasource id related to the Report.

    External Reporting Server Configuration

    You have to configure the external reporting server through ReportingServer property to process the Reports from External Report Server as like below code.

  • c#
  • protected void ExportButton_Click(object sender, EventArgs e)
    {
        try
        {
            reportWriter.ReportingServer = new ReportingServerExt();
            reportWriter.ReportServerCredential = new System.Net.NetworkCredential("guest", "demo");
        }
        catch { }
    }
    		
    public class ReportingServerExt: Syncfusion.EJ.RDL.ServerProcessor.ReportingServer
    {
        // This method will invoked from Report Helper to get the Shared Data Sources from the external Report Server
        public override Syncfusion.EJ.RDL.ServerProcessor.DataSourceDefinition GetDataSourceDefinition(string dataSource)
        {
            return base.GetDataSourceDefinition();
        }
        // This method will invoked from Report Helper to get the reports from the external Report Server
        public override System.IO.Stream GetReport()
        {
            base.GetReport();
        }
    }

    Report Server WebAPI configuration

    You have to use the Report Server WebAPI in GetDataSourceDefinition and GetReport overridden method. For example, get the created ReportingServerExt from the following location.

    http://www.syncfusion.com/downloads/support/directtrac/general/ze/Classes-481585251

    Generating Report Formats

    The ReportWriter provides support for exporting report as PDF, Word, Excel and HTML documents with the help of the ReportWriter class.

    Exporting Report as PDF

    The report used in ReportWriter can be exported as a PDF document using the following code.

  • c#
  • ReportWriter reportWriter = new ReportWriter(reportpath, dataSources);
    reportWriter.Save("Sample.pdf", WriterFormat.PDF);

    Exporting Report as Word

    The report used in ReportWriter can be exported as a Word document using the following code.

  • c#
  • ReportWriter reportWriter = new ReportWriter(reportpath, dataSources);
    reportWriter.Save("Sample.doc", WriterFormat.Word);

    Exporting Report as Excel

    The report used in ReportWriter can be exported as an Excel document using the following code.

  • c#
  • ReportWriter reportWriter = new ReportWriter(reportpath, dataSources);
    reportWriter.Save("Sample.xls", WriterFormat.Excel);

    Exporting Report as HTML

    The report used in ReportWriter can be exported as an HTML document using the following code.

  • c#
  • ReportWriter reportWriter = new ReportWriter(reportpath, dataSources);
    reportWriter.Save("Sample.html", WriterFormat.HTML);