Wednesday, 8 May 2013

Export SSRS report directly to PDF or Excel Format(without Report Viewer)


Export SSRS report directly to PDF or Excel Format(without Report Viewer)



In this post, we are explaining how SSRS report can be directly exported to PDF or Excel.

//Show PDF report accepts 3 parameters, report is the name of the report on the report server, reportParameters is the parameter required for the report to run, fileName is the name in which you want the report to be stored

public static void ShowPdfReport
(string report, Dictionary<stringobject>  reportParameters,string fileName)
  {
    try
     {
      Screen screen;

      //Get the server and the server path from Web config

      string reportUrl =ConfigurationManager.AppSettings["server"];
      reportUrl += "?" +ConfigurationManager.AppSettings["serverPath"];
      string reportParametersQT = String.Empty;
     
     
      foreach (var entry in reportParameters)
      {
        reportParametersQT += "&" + entry.Key + "=" + entry.Value;
      }

/*Creating a web request using the reportUrl,the report,the format in which the report is to be displayed and the report parameters.
Here rs is a built in parameter of Reporting Services and I am instructing reporting services to render(rs:Command=Render)the report in PDF format(rs:Format=PDF),to export the report in Excel all you have to do is change the format to (rs:Format=Excel)*/

WebRequest req = WebRequest.Create(reportUrl + report + "&rs:Command=Render&rs:Format=PDF" + reportParametersQT);

//Passing the credentials of the report server

req.Credentials = newNetworkCredential(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);

//Creating a response object

      WebResponse response = req.GetResponse();
      Stream stream = response.GetResponseStream();
      screen.Response.Clear();
string enCodeFileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);

// The word attachment in Addheader is used to directly show the save dialog box in browser
screen.Response.AddHeader("content-disposition","attachment; filename=" + enCodeFileName);
      screen.Response.BufferOutput = false;   // to prevent buffering
      screen.Response.ContentType = response.ContentType;
      byte[] buffer = new byte[1024];
      int bytesRead = 0;
      while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
       {
          screen.Response.OutputStream.Write(buffer, 0, bytesRead);
       }
       screen.Response.End();
     }
     catch (Exception e)
     {
     }
}

SSRS Excel Export for more than 65536 rows


SSRS Excel Export for more than 65536 rows


When you export data in excel file through SSRS report (up to 2008 R2 ), you generally get into limitation of exporting rows less than 65536 and, if row count exceeds 65536, the report fails to generate the excel file.

Why so ?
SSRS generally renders the excel file in .xls format which has limitation of rows not exceeding more than 65536.

Solution
To overcome this multiple worksheets can be created in excel file. Each worksheet will hold up to 60K rows.

Group and Page breaks
Using groups and page breaks in SSRS report, we can restrict report to hold specific number of data in report per page. Applying page break for the group allows excel to show that many numbers of rows per work sheet.

Steps

  • Design the report as per the requirement.


Group Addition


    Select "Parent Group" from "Add Group" section by right click on the detail row



    Group Expression


      Add group expression “=Ceiling((RowNumber(Nothing)) / <Expected row count>)”
      Put expected row count as the number of row count you want in the excel worksheet. This count should not exceed 65536.













              Once the group and expression is added remove the sort expression from the row group properties.







          •    Page break.

          Now report will show 60K rows per group. Next step is to add the page break.
          From the group properties go to page break and select the option.


          6.      Run the report
          Once you export excel, you will find different worksheets created for data having rows more than 60000.













          How this is handled in later versions.
          The Reporting Services Excel rendering extension, new in SQL Server 2012, renders a report as an Excel document that is compatible with Microsoft Excel 2007-2010 as well as Microsoft Excel 2003 with the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint installed. The format is Office Open XML and the file extension is XLSX. 
          This Excel-rendering extension removes limitations of the earlier version, compatible with Excel 2003. The following lists the improvement in the rendering extension:
          ·         Maximum rows per worksheet is 1,048,576.
          ·         Maximum columns per worksheet is 16,384.