Play with SSRs Article 4-use reports in programs

Source: Internet
Author: User

The report service is a component of Microsoft sqlserver Business Intelligence. With the help of the report service, you can create a variety of reports, whether simple or complex, at the same time, the system provides the subscription function to facilitate users to subscribe to reports. In addition, developers can easily integrate an SSRs report into their own winform or webformProgram.

This article describes how to use the Report Server in an application.

 

SSRs reports can be viewed on the report management interface, and can be easily added to your own applications, this makes it easy for your applications to integrate the report function.

First open Visual Studio 2012. Here we use the latest Visual Studio version for demonstration. The methods in earlier versions have not changed, so you can refer to the methods in this article.

Create an Asp.net project, ASP. NET empty web application.

Add an Asp.net page for the project. Right-click the project name in Solution Explorer and add-> new item...

Select Web form. Click OK.

Click the design button under the webpage design interface to switch the view to the design mode.

In the toolbox, first find the scriptmanager control under Ajax extensions. Subsequent report controls require the support of this basic Ajax control.

Drag the scriptmanager control to the webpage and you will see the placeholder in design mode.

Next, find the reportviewer control in the toolbox and drag it to the webpage design interface.

Click the reportviewer control and click the right arrow to bring up the Property setting interface. Select Server Report for choose report.

Set the Report Server URL attribute to the address of the previously deployed report service instance:

Http: // Wade-PC/reportserver

Note that the address is the reportserver address rather than the reports address.

Then specify the report path.

/Report project1/report1

It must be preceded by a slash (/). The report name does not have the RDL suffix.

Shows the webpage design interface after the settings are complete.

After setting, check the effect. First, right-click the created page in the Asp.net page of Solution Explorer and select set as start page.

Click inter explorer in the toolbar.

The system automatically opens the IE browser to view the Display Effect in the SSRs report in the browser.

 

You can specify parameters for reports in applications. This is a useful function. For example, if the program only wants the user to view the data of the current year, you canCodeDirectly.

For the parameter method in the program, refer to the following code, first Add a reference:

UsingMicrosoft. Reporting. webforms;

Then add the following code to page_load:

Protected VoidPage_load (ObjectSender,EventargsE)

{

If(! Page. ispostback)

{

List<Reportparameter> Paras =New List<Reportparameter> ();

Paras. Add (New Reportparameter("Year","2007"));

Reportviewer1.serverreport. setparameters (paras );

}

}

 

Click Browse. You can see that the parameters are directly written to the report parameters.

In addition, you can add the following statements to prevent the parameters in the report from being displayed.

Reportviewer1.showparameterprompts =False;

 

In addition, report Viewer provides the export function. Sometimes we want to export the report to PDF or Excel directly. The following code can be used for simple implementation:

// 1: excel, other: PDF

IntOuttype = 0;

 

Try

{

// Reportviewer1.processingmode = processingmode. Remote;

 

Warning [] warnings;

String[] Streamids;

StringMimetype;

StringEncoding;

StringFilenameextension;

 

If(Outtype = 1)

{

Byte[] Bytes = reportviewer1.serverreport. Render (

"Excel",Null,OutMimetype,OutEncoding,OutFilenameextension,

OutStreamids,OutWarnings );

 

Response. Clear ();

Response. Buffer =True;

Response. contenttype ="Application/vnd.xls";

Response. addheader ("Content-disposition","Attachment; filename ="+"Report.xls");

 

Response. binarywrite (bytes );

}

Else

{

Byte[] Bytes = reportviewer1.serverreport. Render (

"Pdf",Null,OutMimetype,OutEncoding,OutFilenameextension,

OutStreamids,OutWarnings );

 

Response. Clear ();

Response. Buffer =True;

Response. contenttype ="Application/pdf";

 

Response. binarywrite (bytes );

}

 

Response. End ();

}

Catch(Exception ex)

{

 

}

After running the code, you can see that the report is directly opened in a pdf reader embedded in IE.

When you change the outtype variable to a value other than 1, you can see that the Excel file is automatically generated. In ie10, the download page is displayed.

Click "open" and start the local Excel file. The data exported to the Excel file is displayed.

 

Next, we will briefly demonstrate how to use SSRs reports in winform. The operation method is similar to that in Asp.net. After the winform program is created, find the report viewer control in the toolbar and drag it to the form, then you can set the corresponding attributes just like Asp.net.

Set the corresponding attributes for report viewer, as in Asp.net.

Run the project and you can see the effect of SSRs in winform.

In addition, the method for passing parameters to a report in winform is the same as that in Asp.net, and is not described here.

 

 

Now, using SSRs reports in applications is a simple introduction. You can see that the report viewer control is mainly used in applications, the methods for operating the report viewer are similar. By the way, to deploy a program with this function on the server, check that the report viewer has been installed. The latest report viewer 2012.

Http://www.microsoft.com/en-us/download/details.aspx? Id = 35747

There are a lot of extensions around the report Viewer application. Imagine generating the corresponding Excel or PDF report in the program, there is no need to operate the COM object or itextsharp, you can use the render method of the report viewer of Asp.net to easily generate an Excel or PDF report using a browser.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.