Getting Started with ASP.NET Application

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

Project creation

Following steps illustrate how to add ReportWriter to the ASP.NET application.

Create a new ASP.NET empty web application project by selecting the WEB category from the listed project templates in Microsoft Visual Studio IDE.

Create ASPX page

To create a new Web Forms in the application, follow the below steps.

  1. Right-click on the project and select add.

  2. Click new item and select Web Forms from the listed templates.

Add references

  1. In the solution explorer, right-click the references folder and then select the add reference.

  2. Add the following assemblies and click ok.

    • 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 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, register the below mentioned two namespaces in the web.config file that is present within the views folder and also in the root directory of the 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. Set the following properties to ReportWriter.

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

      • ReportProcessingMode - Set ProcessingMode as remote for RDL and Report Server report as local for RDLC report.

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

    2. Initialize ReportWriter by using the following code example in the Default.aspx.cs page export button click event.

      protected void ExportButton_Click(object sender, EventArgs e)
      {
          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 (this.ExportFormat.SelectedValue == "PDF")
              {
                  fileName = "GroupingAgg.pdf";
                  format = WriterFormat.PDF;
              }
              else if (this.ExportFormat.SelectedValue == "Word")
              {
                  fileName = "GroupingAgg.doc";
                  format = WriterFormat.Word;
              }
              else if (this.ExportFormat.SelectedValue == "Html")
              {
                  fileName = "GroupingAgg.Html";
                  format = WriterFormat.HTML;
              }
              else
              {
                  fileName = "GroupingAgg.xls";
                  format = WriterFormat.Excel;
              }
              reportWriter.Save(fileName, format, httpContext.Response);
          }
          catch { }
      }
    3. Add the following code example in the <body> tag of the Default.aspx 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">
                  <form id="form1" runat="server">
                      <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
                              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%;">
                              <asp:Label Style="font-size: large;" runat="server">
                                  Save As :
                              </asp:Label>
      
                              <asp:RadioButtonList RepeatLayout="Flow" ID="ExportFormat" RepeatDirection="Horizontal" runat="server">
                                  <asp:ListItem Selected="True">PDF</asp:ListItem>
                                  <asp:ListItem>Word</asp:ListItem>
                                  <asp:ListItem>Excel</asp:ListItem>
                                  <asp:ListItem>HTML</asp:ListItem>
                              </asp:RadioButtonList>
      
                              <asp:Button style="width: 18%; margin-left: 2%;" ID="ExportButton" runat="server" OnClick="ExportButton_Click" Text="Generate" />
                          </div>
                      </div>
                  </form>
              </div>
          </div>
      </body>
    4. Add the style to the ReportWriter by using the following code example in the <head> tag of the Default.aspx page.

      <html>
      <head runat="server">
          <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>
    5. Run the application. The output displays ReportWriter export options as shown below. Select the export option and then click the generate.

      Now, the output displays exported report in PDF format.

    Generate RDL reports

    The ReportWriter has options to save the RDL reports. The following code example helps 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 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 is implemented with necessary capabilities to access the contents through WebAPI service. In this tutorial, the following API methods is used from Report Server WebAPI service.

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

    External Reporting Server configuration

    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 can 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);