What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 55390 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Transforming a SSRS(RDLC) Report report output to Excel format

Transforming a SSRS(RDLC) Report report output to Excel format

Article posted by Sksamantaray on 3/27/2012 | Views: 5461 | Category: ASP.NET | Level: Beginner | Points: 250 red flag


In this article we shall learn how to transforming the report output into MS Excel.

Download


 Download source code for Transforming a SSRS(RDLC) Report report output to Excel format


Introduction

Sql Server Reporting service has got a wonderful option called export which in fact helps to export the report output to PDF or Excel. However unfortunately it does not allow user to generate any specific type of output like PDF or excel. So in this article we are going to see how to create only Excel file from the report output.

Objective

To transform a RDLC report output to an Excel file 

Requisites

1.Create a SSRS RDLC Report
2.Create Function Which can convert Report output to Excel file.

Using the code

In this code we are going to create a report to show top 20 employee details.After the report is created, it needs to be converted to Excel file manually.  So in the under mentioned code the export control is removed ,intentionally ,by using  ShowExportControls=false.

The getData() method of the code behind populates employee details in the report-viewer.
When one clicks on Export2Excel button the output of Report-Viewer gets converted to Excel file  and the user is asked to save the file to physical location or directly open it .

   <form id="form1" runat="server">
    <div>
    
        <br />
        &nbsp;<asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
            Text="Export2Excel" />
        <br />
    
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" ShowExportControls="false" 
            Font-Size="8pt" Height="400px" Width="100%">
            <LocalReport ReportPath="Report1.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" 
                        Name="DataSet1_Employees" />
                </DataSources>
            </LocalReport>
        </rsweb:ReportViewer>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetDataItem" TypeName="RDLC_webApplication1._Default">
        </asp:ObjectDataSource>
    
    </div>
    </form>                                                                          

// Code Behind                                                                     
public partial class _Default : System.Web.UI.Page
    {

        string connn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();

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

        protected void getData()
        {

       SqlDataAdapter da = new SqlDataAdapter("select top 20 * from Employees", connn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ReportDataSource rdc = new ReportDataSource("DataSet1_Employees", ds.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rdc);
            ReportViewer1.Visible = true;
            ReportViewer1.LocalReport.Refresh();
        }
    }                                                                                //To generate Excel file                                                              protected void Button2_Click(object sender, EventArgs e)
    {

        Response.Clear();
        Microsoft.Reporting.WebForms.Warning[] warnings = null;
        string[] streamids = null;
        String mimeType = null;
        String encoding = null;
        String extension = null;
        Byte[] bytes = null;

        bytes = ReportViewer1.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);
        Response.Clear();
        Response.Buffer = true;
        Response.Clear();

        Response.AddHeader("Content-Disposition", "attachment; filename=abc.xls");
        Response.AddHeader("Content-Length", bytes.Length.ToString());
        Response.BinaryWrite(bytes);
        Response.End();
    }  

When user first loads the page ,the getData() method is called,which takes care of loading employee details in the report-viewer. Here you cannot see any export option since it is made to false as i mention earlier in this article. To create an Excel file  , user needs to click on Export2Excel button. 




 When user clicks on Export2Excel ,the following screen asks to open or save the file to physical location.




Conclusion

In this way an Excel file  can be created manually  from SSRS- RDLC Report output. 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:5 year(s)
Home page:
Member since:Sunday, November 06, 2011
Level:Silver
Status: [Member]
Biography:Scalable-Systems Pvt Ltd
>> Write Response - Respond to this post and get points
Related Posts

Many times we want to implement pre-processing logic before a request hits the IIS resources. For instance you would like to apply security mechanism, URL rewriting, filter something in the request etc. ASP.NET has provided two types of interception HttpModule and HttpHandler. This article walks through it.

Several time while developing asp .NET applications we need to transfer daa from one page to another, we can achieve this by several methods , some of them are 1. Using Query String 2. Using Cookies 3. Using Session Variables 4. Using Cross Page Posting 5 . Using Server.Transfer

We have 100 records in gridview and we want to select 90th record. But user is not intersted to scroll through mouse and see that record. So in my article I have added linkbuttons dynamically using placeholder. When I click 90th record automatically scroll to 90th record and display it.

Some times back I was discussing MVC with one of my Java friends. The talk ended up with a fight trying to prove how one technology is better than other in implementing MVC. For whatever reasons good or bad I was trying to prove that Microsoft technology is the best but…hmm,aaahh and ooohh.

This post is just a guide/approach for the bill desk payment gateway integration.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 9:55:59 AM