Orice aplicatie trebuie sa aiba posibilitatea de a tipari
datele. Pentru aceasta in VS2005 Express se poate folosi componenta Report
Viewer care e free si se poate downloada de la http://www.gotreportviewer.com.
Dupa ce o instalati, o sa aveti in toolbox
urmatoarea componenta:
Acum adaugam un raport care sa fie afisat
de catre aplicatie – deschidem aplicatia Web si adaugam un nou item de tipul
Report si il numim rptPublisher.rdlc :
Acum adaugam un nou datasource apasand pe
Add New DataSource
In ecranul urmator apasam pe NewConnection
si selectam baza noastra de date:
Putem salva conexiunea in Web.Config(desi o
mai avem) si apasam Next. Pe urmatorul ecran lasam selectat “Use SQL
Statements” si iarasi Next. Acum scriem codul pentru SQL:
SELECT IDPublisher, NamePublisher,
SitePublisher
FROM Publisher
si iarasi Next pina se termina ( sau direct
Finish).
Acum in WebSite DataSource ne-a aparut
dataset-ul care contine Publisher
Ne ducem pe Toolbox si tragem pe rptPublisher.rdlc
un Table
Ne intoarcem pe WebSite DataSources si
tragem Name Publisher pe detaliu:
Acum sa verificam
Cream o noua pagina aspx, frmPublisherReport.aspx
si scriem urmatorul cod(sau tragem controlul de ReportViewer si ii spunem care
e raportul)
<%@ Page Language=”C#” MasterPageFile=”~/Book.master” AutoEventWireup=”true” CodeFile=”frmPublisherReport.aspx.cs” Inherits=”frmPublisherReport”
Title=”Report
Publisher” %>
<%@ Register Assembly=”Microsoft.ReportViewer.WebForms, Version=8.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
Namespace=”Microsoft.Reporting.WebForms” TagPrefix=”rsweb”
%>
<asp:Content ID=”Content1″
ContentPlaceHolderID=”ContentPlaceHolder1″
Runat=”Server”>
<rsweb:ReportViewer ID=”rptPublisher” runat=”server”>
<LocalReport ReportPath=”rptPublisher.rdlc”>
</LocalReport>
</rsweb:ReportViewer>
</asp:Content>
In codul C# punem urmatoarele linii :
using System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
Microsoft.Reporting.WebForms;
public partial class frmPublisherReport : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
BookObjects.ColPublisher publishers = new
BookObjects.ColPublisher();
publishers.Load();
ReportDataSource
rds = new ReportDataSource(“DataSet1_Publisher”,publishers);
rptPublisher.ProcessingMode
= ProcessingMode.Local;
rptPublisher.LocalReport.DataSources.Add(rds);
rptPublisher.LocalReport.Refresh();
}
}
Si modificam fisierul rdlc deschizindu-l cu
notepad-ul si scriind urmatoarele:
<?xml version=”1.0″
encoding=”utf-8″?>
<Report xmlns=”http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition”
xmlns:rd=”http://schemas.microsoft.com/SQLServer/reporting/reportdesigner”>
<DataSources>
<DataSource
Name=”BookConnectionString”>
<ConnectionProperties>
<ConnectString />
<DataProvider>SQL</DataProvider>
</ConnectionProperties>
<rd:DataSourceID>c649d533-64c3-42b6-9805-19adbfccd468</rd:DataSourceID>
</DataSource>
</DataSources>
<BottomMargin>1in</BottomMargin>
<RightMargin>1in</RightMargin>
<rd:DrawGrid>true</rd:DrawGrid>
<InteractiveWidth>8.5in</InteractiveWidth>
<rd:SnapToGrid>true</rd:SnapToGrid>
<Body>
<ReportItems>
<Table Name=”table1″>
<Footer>
<TableRows>
<TableRow>
<TableCells>
<TableCell>
<ReportItems>
<Textbox
Name=”textbox7″>
<rd:DefaultName>textbox7</rd:DefaultName>
<ZIndex>1</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
<Height>0.45833in</Height>
</TableRow>
</TableRows>
</Footer>
<Left>0.5in</Left>
<DataSetName>DataSet1_Publisher</DataSetName>
<Top>0.25in</Top>
<Width>6.5in</Width>
<Details>
<TableRows>
<TableRow>
<TableCells>
<TableCell>
<ReportItems>
<Textbox
Name=”txtName”>
<rd:DefaultName>Name</rd:DefaultName>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>=Fields!Name.Value</Value>
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
<Height>0.45833in</Height>
</TableRow>
</TableRows>
</Details>
<Header>
<TableRows>
<TableRow>
<TableCells>
<TableCell>
<ReportItems>
<Textbox
Name=”textbox1″>
<rd:DefaultName>textbox1</rd:DefaultName>
<ZIndex>2</ZIndex>
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
</Style>
<CanGrow>true</CanGrow>
<Value>Name</Value>
</Textbox>
</ReportItems>
</TableCell>
</TableCells>
<Height>0.45833in</Height>
</TableRow>
</TableRows>
</Header>
<TableColumns>
<TableColumn>
<Width>6.5in</Width>
</TableColumn>
</TableColumns>
<Height>1.375in</Height>
</Table>
</ReportItems>
<Height>2in</Height>
</Body>
<rd:ReportID>9f0247a8-15fe-4ef9-962e-c4c670524163</rd:ReportID>
<LeftMargin>1in</LeftMargin>
<DataSets>
<DataSet
Name=”DataSet1_Publisher”>
<rd:DataSetInfo>
<rd:ObjectDataSourceType>Publisher</rd:ObjectDataSourceType>
<rd:TableName>Publisher</rd:TableName>
</rd:DataSetInfo>
<Query>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
<CommandText />
<DataSourceName>BookConnectionString</DataSourceName>
</Query>
<Fields>
<Field
Name=”IDPublisher”>
<rd:TypeName>System.Int32</rd:TypeName>
<DataField>IDPublisher</DataField>
</Field>
<Field Name=”Name”>
<rd:TypeName>System.String</rd:TypeName>
<DataField>Name</DataField>
</Field>
<Field Name=”Site”>
<rd:TypeName>System.String</rd:TypeName>
<DataField>Site</DataField>
</Field>
</Fields>
</DataSet>
</DataSets>
<Width>9.75in</Width>
<InteractiveHeight>11in</InteractiveHeight>
<Language>en-US</Language>
<TopMargin>1in</TopMargin>
</Report>
Acum , rulind pagina, observam ca se poate
exporta raportul in Excel si PDF – mai mult decit sufficient:
Data viitoare vom vedea acelasi raport in WindowsForms…
Linkuri utile:
http://www.gotreportviewer.com/ – in partea dreapta aveti exemple de aplicatii care fac diverse –
genereaza automat fisierele rdlc, scot automat fisierele Excel si multe altele
SQL Server Reporting Services – pentru
utilizarea avansata a rapoartelor.