借助于repeater实现自动分页的WEB打印功能

07年的一篇文章转来等待过几天测试

 

 

刚才在洗衣服时,我在想:好久,我没有动“笔”写东西了,脑子也开始好像慢慢地缺乏了整理,像我们自己用久的了硬盘一样,性能日趋下降;从两年前用了PETSHOP1.0模式给某政府单位开发了一个网络OA后,我再也没有很认真来考虑一些真正的关于.Net技术的问题,更不提现在的什么AJAX。自从去年前面的10个月的数据仓库项目开始后到现在,我觉得自己总是在忙忙碌碌,难得有闲时梳理自己的某些想法,越发觉得懒惰。Maybe,这是麻木的开始…….

       前段时间,哥们接了个车辆管理系统,B/S架构的,他做了大部分,后到了北京去开某站点的垂直搜索引擎后,这单事落到了俺的身上(我在西南某个漂亮的地方)。废话有点多了,久不写了,思绪的点飘。。。哈哈
话归正题。在做之前,我先说下客户的要求:需要实现的功能很简单,就是统计本单位每一辆车的各种费用以及出车次数、事故次数。刚开始的思路是这样的,先在EXCEL定义好格式与各种统计指标,如这样<%#Department%> 表示部门,然后在逻辑上再以一个同名(为什么是同名呢?只是便于统计实体的映射了)的Department方法来实现数据的输出,通过遍历这标签(统计指标)我们就可以实现了EXCEL报表。但后来,因为实施过程碰到了些不确定因素,再加上客户说,每次启动EXCEL太慢了,这个问题也确实存在,再加数据的运算,这个方法在使用上对于客户来说是要有点耐心呢~~。
    以前自己也用个控件实现个WEB的打印,但这个方案同样不适用于本项目,因为客户的计算机使用水平相对有点低,设置起来对于他们来说很麻烦,因此这个办法—Get Out! So,改!于是,我到网上看看有些什么东东我可以用的~~,一步入“江湖”,好不热闹!某些BadBoy说用WebBrowser做打印不错呢~,于是我也凑起热闹了。
找齐家伙:
///////////////////////////////// 负责打印的家伙,我们可以将它放在打印表格的底部,我稍稍了解了一下它的用法,从网上Copy的,名字都没改~~,我也不知大概的出处在那里,没法注明这位仁兄的大名,不好意思了
<script language="javascript">
<!--//
              function printsetup(){
                     // 打印页面设置
                     wb.execwb(8,1);
              }
              function printpreview()
              {
                     // 打印页面预览
                     wb.execwb(7,1);
              }
 
              function printit()
              {
                     if (confirm('确定打印吗?')) {
                     wb.execwb(6,6)
              }
       }
//-->
</script>
<OBJECT id=wb height=0 width=3
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 name=wb >
</OBJECT>
<INPUT class= Noneadprinting onclick=javascript:printit() type=button value=打印 name=button_print>
<INPUT class= Noneadprinting onclick=javascript:printsetup(); type=button value=打印页面设置 name=button_setup>
<INPUT class= Noneadprinting onclick=javascript:printpreview(); type=button value=打印预览 name=button_show>
<input class= Noneadprinting onclick=javascript:window.close(); type=button value=关闭 name=button>
/////////////////////////////////表格样式,什么表头呀,每一行呀,ect.
<style type=text/css>
       .report_head { FONT-WEIGHT: bold; FONT-SIZE: 18pt; COLOR: #000000; TEXT-DECORATION: none }
       .report_column { BORDER-TOP: #000000 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 12pt; COLOR: #014e82; TEXT-DECORATION: none }
       TD { BORDER-RIGHT: #ffffff 0px solid; BORDER-TOP: #ffffff 0px solid; FONT-SIZE: 11pt; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid }
       TableHead { BORDER-LEFT-COLOR: #000000; BORDER-BOTTOM-COLOR: #000000; BORDER-TOP-COLOR: #000000; BORDER-COLLAPSE: collapse; BORDER-RIGHT-COLOR: #000000 }
</style>
/////////////////////////////////屏蔽多余的东东
<style media=print>
       .Noneadprinting { DISPLAY: none }  
       .PageNext { PAGE-BREAK-AFTER: always } //这东西很可爱,分页就要靠它了~~
</style>
基于以上的东东,我们基本上可以做打印了。
我把先搭了个Frame:
在form里写好表头:
<DIV class=report_head align=center>西电实业有限公司汽车运输分公司车辆使用统计报表</DIV>
接着定好统计项,装载数据等:
<TABLE class=TableHead id=Table1 cellSpacing=0 cellPadding=3 width="100%" 
border
=1>
 
                                   
<TR class="report_column">
                                          
<td align=center width=165 height=19>部门</td>
                                          
<td style="WIDTH: 193pt" align=center>车牌号码</td>
                                          
<td style="WIDTH: 179pt" align=center>车型</td>
                                          
<td style="WIDTH: 188pt" align=center>行程里程</td>
                                          
<td style="WIDTH: 120pt" align=center>总油耗</td>
                                          
<td style="WIDTH: 40pt" align=center>加油费</td>
                                          
<td style="WIDTH: 140pt" align=center>维修费</td>
                                          
<td style="WIDTH: 122pt" align=center>养路费</td>
                                          
<td style="WIDTH: 180pt" align=center>过路/桥费</td>
                                          
<td style="WIDTH: 54pt" align=center>洗车费</td>
                                          
<td style="WIDTH: 54pt" align=center>停车费</td>
                                          
<td style="WIDTH: 54pt" align=center>年检费</td>
                                          
<td style="WIDTH: 54pt" align=center>保险费</td>
                                          
<td style="WIDTH: 59pt" align=center>其它费用</td>
                                          
<td style="WIDTH: 40pt" align=center>总费用</td>
                                          
<td style="WIDTH: 150pt" align=center>出车次数</td>
                                          
<td style="WIDTH: 59pt" align=center>运营费用</td>
                                          
<td style="WIDTH: 59pt" align=center>事故次数</td>
                                          
<td style="WIDTH: 143pt" align=center>利用天数</td>
                                          
<td style="WIDTH: 80pt" align=center>利用率</td>
                                   
</TR>     
                                   
                            
                     
<asp:repeater id=vehiclesGrid runat="server">
                     
<ItemTemplate>
                            
<TR>
                                   
<TD height="5" width="165"><%# DataBinder.Eval(Container.DataItem, "Department.Name"%></TD>
                                   
<TD height="5" style='WIDTH:193pt'><%# DataBinder.Eval(Container.DataItem, "Number"%></TD>
                                   
<TD style='WIDTH:179pt'><%# DataBinder.Eval(Container.DataItem, "Type.TypeName"%></TD>
                                   
<TD style='WIDTH:188pt'><%# DataBinder.Eval(Container.DataItem, "MilesRange"%></TD>
                                   
<TD style='WIDTH:120pt'><%# DataBinder.Eval(Container.DataItem, "OilCountSum"%></TD>
                                   
<TD style='WIDTH:40pt'><%# DataBinder.Eval(Container.DataItem, "GasCostSum"%></TD>
                                   
<TD style='WIDTH:140pt'><%# DataBinder.Eval(Container.DataItem, "MaintainCostSum"%></TD>
                                   
<TD style='WIDTH:122pt'><%# DataBinder.Eval(Container.DataItem, "MaitainRoadAndBridgeSum"%></TD>
                                   
<TD style='WIDTH:180pt'><%# DataBinder.Eval(Container.DataItem, "RoadAndBridgeTollSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "WashCarCostSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "ParkCarCostSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "YearCheckCostSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "InsuranceCostSum")%></TD>
                                   
<TD style='WIDTH:59pt'><%# DataBinder.Eval(Container.DataItem, "ElseCostSum")%></TD>
                                   
<TD style='WIDTH:40pt'><%# DataBinder.Eval(Container.DataItem, "CostSum")%></TD>
                                   
<TD style='WIDTH:150pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "AssignmentCount")%>
                                   
</TD>
                                   
<TD style='WIDTH:59pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "RunCost")%>
                                   
</TD>
                                   
<TD style='WIDTH:59pt'><%# DataBinder.Eval(Container.DataItem, "AccidentCounts")%></TD>
                                   
<TD style='WIDTH:143pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "AssignmentDays")%>
                                   
</TD>
                                   
<TD style='WIDTH:80pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "AssignmentDaysRate")%>
                                   
</TD>
                            
</TR>
                     
</ItemTemplate>
              
</asp:repeater>
       
</TABLE>
通过repeater的模板属性就已经实现了多记录的打印的功能,但我们的分页呢?在那呢?此我就作了以下的改动:
 
<script runat=server>
       public 
int nPageSize =10; //每页要打页的记录数
       public 
int nRecordNum=0/ /作为分页点用
</script>
<TABLE class=TableHead id=Table1 cellSpacing=0 cellPadding=3 width="100%" 
border
=1>
<%
 
if    (nRecordNum<=nPageSize)
                                   {
                            
%>
                     
                                   
<TR class="report_column">
                                          
<td align=center width=165 height=19>部门</td>
                                          
<td style="WIDTH: 193pt" align=center>车牌号码</td>
                                          
<td style="WIDTH: 179pt" align=center>车型</td>
                                          
<td style="WIDTH: 188pt" align=center>行程里程</td>
                                          
<td style="WIDTH: 120pt" align=center>总油耗</td>
                                          
<td style="WIDTH: 40pt" align=center>加油费</td>
                                          
<td style="WIDTH: 140pt" align=center>维修费</td>
                                          
<td style="WIDTH: 122pt" align=center>养路费</td>
                                          
<td style="WIDTH: 180pt" align=center>过路/桥费</td>
                                          
<td style="WIDTH: 54pt" align=center>洗车费</td>
                                          
<td style="WIDTH: 54pt" align=center>停车费</td>
                                          
<td style="WIDTH: 54pt" align=center>年检费</td>
                                          
<td style="WIDTH: 54pt" align=center>保险费</td>
                                          
<td style="WIDTH: 59pt" align=center>其它费用</td>
                                          
<td style="WIDTH: 40pt" align=center>总费用</td>
                                          
<td style="WIDTH: 150pt" align=center>出车次数</td>
                                          
<td style="WIDTH: 59pt" align=center>运营费用</td>
                                          
<td style="WIDTH: 59pt" align=center>事故次数</td>
                                          
<td style="WIDTH: 143pt" align=center>利用天数</td>
                                          
<td style="WIDTH: 80pt" align=center>利用率</td>
                                   
</TR>     
                                   
                            
<%
                                   }
%>
                     
<asp:repeater id=vehiclesGrid runat="server">
                     
<ItemTemplate>
                     
                     
                                   
<%
                                   nRecordNum
++;
                                   
                                   
int nModPage=0;
                                   Math.DivRem( nRecordNum ,nPageSize ,out nModPage);
                                   
if    (nModPage+1==1)
                                   {
                            
%>
                                   
<!-―分页标志begin――>
                                   
<TR class="PageNext">
                                          
<td colspan=20 class="PageNext" height="0"></td></TR>
                                   
<!-―分页标志end――>
 
                                          
                            
<TR class="report_column">
                                          
                                          
<td class="report_column" align=center width=165 height=19>部门</TD>
                                          
<td class="report_column" style="WIDTH: 193pt" align=center>车牌号码</TD>
                                          
<td class="report_column" style="WIDTH: 179pt" align=center>车型</TD>
                                          
<td class="report_column" style="WIDTH: 188pt" align=center>行程里程</TD>
                                          
<td class="report_column" style="WIDTH: 120pt" align=center>总油耗</TD>
                                          
<td class="report_column" style="WIDTH: 40pt" align=center>加油费</TD>
                                          
<td class="report_column" style="WIDTH: 140pt" align=center>维修费</TD>
                                          
<td class="report_column" style="WIDTH: 122pt" align=center>养路费</TD>
                                          
<td class="report_column" style="WIDTH: 180pt" align=center>过路/桥费</TD>
                                          
<td class="report_column" style="WIDTH: 54pt" align=center>洗车费</TD>
                                          
<td class="report_column" style="WIDTH: 54pt" align=center>停车费</TD>
                                          
<td class="report_column" style="WIDTH: 54pt" align=center>年检费</TD>
                                          
<td class="report_column" style="WIDTH: 54pt" align=center>保险费</TD>
                                          
<td class="report_column" style="WIDTH: 59pt" align=center>其它费用</TD>
                                          
<td class="report_column" style="WIDTH: 40pt" align=center>总费用</TD>
                                          
<td class="report_column" style="WIDTH: 150pt" align=center>出车次数</TD>
                                          
<td class="report_column" style="WIDTH: 59pt" align=center>运营费用</TD>
                                          
<td class="report_column" style="WIDTH: 59pt" align=center>事故次数</TD>
                                          
<td class="report_column" style="WIDTH: 143pt" align=center>利用天数</TD>
                                          
<td class="report_column" style="WIDTH: 80pt" align=center>利用率</TD>
                            
</TR>            
                            
<%
                                   }
                            
else
                                   {
%>
                            
<TR><%
                                   }
%>
                                   
<TD height="5" width="165"><%# DataBinder.Eval(Container.DataItem, "Department.Name"%></TD>
                                   
<TD height="5" style='WIDTH:193pt'><%# DataBinder.Eval(Container.DataItem, "Number"%></TD>
                                   
<TD style='WIDTH:179pt'><%# DataBinder.Eval(Container.DataItem, "Type.TypeName"%></TD>
                                   
<TD style='WIDTH:188pt'><%# DataBinder.Eval(Container.DataItem, "MilesRange"%></TD>
                                   
<TD style='WIDTH:120pt'><%# DataBinder.Eval(Container.DataItem, "OilCountSum"%></TD>
                                   
<TD style='WIDTH:40pt'><%# DataBinder.Eval(Container.DataItem, "GasCostSum"%></TD>
                                   
<TD style='WIDTH:140pt'><%# DataBinder.Eval(Container.DataItem, "MaintainCostSum"%></TD>
                                   
<TD style='WIDTH:122pt'><%# DataBinder.Eval(Container.DataItem, "MaitainRoadAndBridgeSum"%></TD>
                                   
<TD style='WIDTH:180pt'><%# DataBinder.Eval(Container.DataItem, "RoadAndBridgeTollSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "WashCarCostSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "ParkCarCostSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "YearCheckCostSum")%></TD>
                                   
<TD style='WIDTH:54pt'><%# DataBinder.Eval(Container.DataItem, "InsuranceCostSum")%></TD>
                                   
<TD style='WIDTH:59pt'><%# DataBinder.Eval(Container.DataItem, "ElseCostSum")%></TD>
                                   
<TD style='WIDTH:40pt'><%# DataBinder.Eval(Container.DataItem, "CostSum")%></TD>
                                   
<TD style='WIDTH:150pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "AssignmentCount")%>
                                   
</TD>
                                   
<TD style='WIDTH:59pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "RunCost")%>
                                   
</TD>
                                   
<TD style='WIDTH:59pt'><%# DataBinder.Eval(Container.DataItem, "AccidentCounts")%></TD>
                                   
<TD style='WIDTH:143pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "AssignmentDays")%>
                                   
</TD>
                                   
<TD style='WIDTH:80pt'>
                                          
<%# DataBinder.Eval(Container.DataItem, "AssignmentDaysRate")%>
                                   
</TD>
                            
</TR>
                     
</ItemTemplate>
              
</asp:repeater>
       
</TABLE>
到此,打印全部结速!简单的打印的功能几乎可以满足了,对于那种说用户自定义打印记录数等等,那个方法还得完善。示例图如下:
posted @ 2009-11-10 12:18  子夜.  Views(2653)  Comments(1Edit  收藏  举报