代码改变世界

Reports Starter Kit详细介绍(一)

2006-05-26 21:28  Clingingboy  阅读(2434)  评论(0编辑  收藏  举报
        Reports Starter Kit八个报表中难易度各不相同.所以我打算从简单的开始入手.先来讲这三个报表

八个报表全部采用三层架构来实现,里面有一个SqlHelper的辅助类(数据访问层).

每个报表都有两个类

一个是业务逻辑类,一个是数据集合类.



每张报表都分两种模式.一种是普通的.一种是打印版本,通过参数Print来判断

所以下面的这段代码是通用的

private void Page_Load(object sender, System.EventArgs e)
        
{
            
if (!IsPostBack)
                BindList();

            
// switches the style sheet based on printer friendly view or not
            if (Request.QueryString["Print"]=="true")
            
{
                _styleSheet 
= "stylesPrint.css";
                PrintButton.Visible 
= true;
            }

            
else 
            
{
                _styleSheet 
= "styles.css";
            }

        }


我个人认为下面三个表是比较简单的

1.表格式报表

2.简易报表

3.文本报表

下面详细来说明如何实现三种报表

一.表格式报表

表格式报表是通过各个类显示各类的不同商品.用到了其中两张表
(1)产品分类表
(2)产品详细信息
实现这样的效果.用到了DataList镶套DataGrid的方法

<asp:datalist id="CategoriesList" runat="server" RepeatDirection="Vertical" repeatcolumns="1"
                Width
="100%" CellPadding="5" cellspacing="15">
                
<itemtemplate>
                    
<table border="0" cellpadding="3" cellspacing="0" class="Content" width="100%">
                        
<tr>
                            
<td valign="top" class="CategoryHeader">
                            
<!-- 类别 -->
                                Category
                                
<%# DataBinder.Eval(Container.DataItem, "CategoryName"%>
                            
</td>
                        
</tr>
                        
<tr>
                            
<td>
                            
<!-- 具体类别产品 -->
                                
<asp:datagrid id=Datagrid1 runat="server" autogeneratecolumns="False" DataSource='<%# GetDetails((int)DataBinder.Eval(Container.DataItem, "CategoryID")) %>' OnItemDataBound="CalculateExtendedPrice" BorderWidth="0" GridLines="None" AllowSorting="true" OnSortCommand="SortGrid" Width="100%">
                                    
<columns>
                                        
<asp:boundcolumn DataField="ProductName" HeaderText="Product" SortExpression="ProductName" ItemStyle-CssClass="ItemStyle"
                                            HeaderStyle-CssClass
="ProductHeader" ItemStyle-Width="200"></asp:boundcolumn>
                                        
<asp:boundcolumn DataField="UnitsInStock" HeaderText="Units In Stock" ItemStyle-CssClass="ItemStyleRight"
                                            HeaderStyle-CssClass
="ProductHeaderRight" SortExpression="UnitsInStock"></asp:boundcolumn>
                                        
<asp:templatecolumn>
                                            
<itemtemplate>
                                                
<img src="images/spacer.gif" width="70" height="1" />
                                            
</itemtemplate>
                                        
</asp:templatecolumn>
                                        
<asp:boundcolumn DataField="QuantityPerUnit" HeaderText="Quantity Per Unit" ItemStyle-CssClass="ItemStyle"
                                            HeaderStyle-CssClass
="ProductHeader"></asp:boundcolumn>
                                        
<asp:boundcolumn DataField="UnitPrice" HeaderText="Unit Price" ItemStyle-CssClass="ItemStyleRight"
                                            HeaderStyle-CssClass
="ProductHeaderRight" SortExpression="UnitPrice"></asp:boundcolumn>
                                        
<asp:boundcolumn HeaderText="Extended Price" ItemStyle-CssClass="ItemStyleRight" HeaderStyle-CssClass="ProductHeaderRight"></asp:boundcolumn>
                                    
</columns>
                                
</asp:datagrid>
                            
</td>
                        
</tr>
                        
<tr>
                            
<td class="CategoryFooter">
                                Total Units in Stock:
                                
<%# DataBinder.Eval(Container.DataItem, "TotalInStock"%>
                            
</td>
                        
</tr>
                    
</table>
                
</itemtemplate>
                
<headerstyle cssclass="ReportTitle"></headerstyle>
            
</asp:datalist>

2.排序

private void SortGridData(TabularReportCollection list, string sortField, bool asc)
        
{
            TabularReportCollection.TabularReportFields sortCol 
= TabularReportCollection.TabularReportFields.InitValue;

            
switch(sortField)
            
{
                
case "ProductName":
                    sortCol 
= TabularReportCollection.TabularReportFields.ProductName;
                    
break;
                ..
            list.Sort(sortCol, asc);
        }

 
TabularReportCollection类继承了ArrayList.它定义了一组枚举用于排序的选择.根据排序字段执行Sort方法.Sort方法支持升序和降序,当升序时,点击时为降序,降序时则相反

//按不同排序字段执行不同排序方法
        public void Sort(TabularReportFields sortField, bool isAscending)
        
{
            
switch (sortField) 
            
{
                
case TabularReportFields.ProductName:
                    
//按产品名排序
                    base.Sort(new ProductNameComparer());
                    
break;
                
case TabularReportFields.UnitsInStock:
                    
//按库存数排序
                    base.Sort(new UnitsInStockComparer());
                    
break;
                
case TabularReportFields.UnitPrice:
                    
//按单价排序
                    base.Sort(new UnitPriceComparer());
                    
break;
            }

            
//升序还是降序
            if (!isAscending) base.Reverse();
        }


3.价格计算和缺货提示

Extended Price 用以计算Units In StockUnit Price的积.当Units In Stock为0时,行显示红色.提示无库存,用改变CSS样式来实现



protected void CalculateExtendedPrice(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        
{
            
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            
{
                
//读取数量和单价
                double unitsInStock = Convert.ToDouble(e.Item.Cells[1].Text);
                
double unitPrice =  Convert.ToDouble(e.Item.Cells[4].Text);
                
//计算商品总价格
                e.Item.Cells[5].Text = string.Format("{0:c}", unitPrice * unitsInStock);

                
// 格式化货币
                e.Item.Cells[4].Text = string.Format("{0:c}", unitPrice);

                
// 如某商品没有库存,则改变其样式,使得它所在的样式边红色
                if (unitsInStock == 0)
                    e.Item.CssClass 
= "OutOfStock";
            }

        }


二.简易报表

简易报表是用一个DataGrid来实现的用来显示客户信息

实现方法跟上面的表格式报表里镶套的DataGrid差不多.实现了排序,交替颜色.只不过它多了分页功能,而其实这个功能却非常的简单

private void CustomerGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        
{
            CustomerGrid.CurrentPageIndex 
= e.NewPageIndex;
            BindGrid();
        }

在打印模式中,还实现了这样一个功能:是否显示分页

private void pagingButton_Click(object sender, System.EventArgs e)
        
{
            CustomerGrid.AllowPaging 
= !CustomerGrid.AllowPaging;    
            BindGrid();

            PagingButton.Text 
= CustomerGrid.AllowPaging ? "Disable Paging" : "Enable Paging";
        }




三.文本报表





文本报表使用了repeater控件,我想这个报表的实现方法是最简单的了.只要绑定数据就好了

<asp:repeater id="EmployeesList" runat="server">
                                
<headertemplate>
                                    
<table border="0" cellpadding="3" cellspacing="0" width="100%">
                                
</headertemplate>
                                
<itemtemplate>
                                    
<tr>
                                        
<td colspan="4" valign="top" class="HeaderStyleText">
                                            Employee:
                                            
<%# DataBinder.Eval(Container.DataItem, "EmployeeName"%>
                                        
</td>
                                        
<td class="HeaderStyleText"></td>
                                    
</tr>
                                    
<tr>
                                        
<td width="50px" class="CategoryFooter">&nbsp;</td>
                                        
<td valign="top" class="CategoryFooter" width="250">
                                            
<%# DataBinder.Eval(Container.DataItem, "EmployeeTitle"%>
                                        
</td>
                                        
<td valign="top" class="CategoryFooter" width="200">
                                            
<%# DataBinder.Eval(Container.DataItem, "EmployeeAddress"%>
                                            
<br>
                                            
<%# DataBinder.Eval(Container.DataItem, "EmployeeCity"%>
                                            ,
&nbsp;<%# DataBinder.Eval(Container.DataItem, "EmployeeState"%>&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "EmployeeZip"%>
                                        
</td>
                                        
<td valign="top" align="left" class="CategoryFooter" width="100">
                                            ext.
                                            
<%# DataBinder.Eval(Container.DataItem, "EmployeeExt"%>
                                        
</td>
                                        
<td width="50px" class="CategoryFooter">&nbsp;</td>
                                    
</tr>
                                    
<tr height="1">
                                        
<td colspan="5" valign="top" class="HeadSeparator">
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td></td>
                                        
<td colspan="3"><%# DataBinder.Eval(Container.DataItem, "EmployeeNotes"%>
                                        
</td>
                                        
<td></td>
                                    
</tr>
                                
</itemtemplate>
                                
<separatortemplate>
                                    
<tr height="1">
                                        
<td colspan="5" class="Headseparator">
                                        
</td>
                                    
</tr>
                                    
<tr height="20">
                                        
<td colspan="5" class="separator">
                                        
</td>
                                    
</tr>
                                
</separatortemplate>
                                
<footertemplate>
            
</table>
            
</footertemplate> </asp:repeater>


private void BindList()
        
{
            EmployeesList.DataSource 
= Components.TextReport.GetEmployees();
            EmployeesList.DataBind();
        }


.repeater控件最大的好处就是可以自定义HTML格式.当然还包括一些模板.但我感觉还是DataList强大一点吧.

以上就是报表中几个简单的例子,相信大家这些都应该会的.如果有错误希望大家指出.

还有今天比较郁闷,一直在学asp.net,在实习公司看到的全部是WinForm的程序,郁闷死了