function QueryString(sName) { var sSource = String(window.document.location); var sReturn = ""; var sQUS = "?"; var sAMP = "&"; var sEQ = "="; var iPos;
iPos = sSource.indexOf(sQUS);
var strQuery = sSource.substr(iPos, sSource.length - iPos); var strLCQuery = strQuery.toLowerCase(); var strLCName = sName.toLowerCase();
.NextPage{page-break-after: always;}
</style>数据1
数据2
...
数据5
<div class="NextPage"></div>
数据6
...
数据10
这样就是分页打印了,到一个换页的地方插入一个<div class="NextPage"></div>
从数据库中获取的结果集为AA[m][n];<table>
<tr>
<td colspan="5">表头</td>
</tr>
<tr>
<td>AA[0][0]</td>
<td>AA[0][1]</td>
...
...
<td>AA[0][n]</td>
</tr>
<tr>
<td>AA[1][0]</td>
<td>AA[1][1]</td>
...
...
<td>AA[1][n]</td>
</tr>
<tr>
<td>AA[2][0]</td>
<td>AA[2][1]</td>
...
...
<td>AA[2][n]</td>
</tr>
....
...
<tr>
<td>AA[5][0]</td>
<td>AA[5][1]</td>
...
...
<td>AA[5][n]</td>
</tr></table>
以上是表的格式,要把数据按照上面的格式打印出来,每个表格一张票据。但现在还存在一个问题,那就是当m<5的还有当m/5不能整除的这两种情况该如何处理。
ASPX部分:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Bak_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<style media=print>
.NextPage{page-break-after: always;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
CS部分: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;public partial class Bak_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//m为数据集总量,nextPager为每5条数据产生一个table并换页
int m = 27, nextPager = 5;
//构造一个datatable,并为其弧值
DataTable dtb = new DataTable();
dtb.Columns.Add(new DataColumn("dataColumn1", typeof(string)));
dtb.Columns.Add(new DataColumn("dataColumn2", typeof(string)));
dtb.Columns.Add(new DataColumn("dataColumn3", typeof(string)));
DataRow dr;
for (int i = 0; i < m; i++)
{
dr = dtb.NewRow();
dr[0] = i + "_" + 0;
dr[1] = i + "_" + 1;
dr[2] = i + "_" + 2;
dtb.Rows.Add(dr);
}
//取数据并输出打印
//创建一个表格
Table tab = new Table();
for (int i = 0; i < dtb.Rows.Count; i++)
{
//创建一行
TableRow tr = new TableRow();
//创建单元格
for (int j = 0; j < dtb.Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = dtb.Rows[i][j].ToString();
cell.Width = 100;
tr.Cells.Add(cell);//添加列到行
}
tab.Rows.Add(tr);//添加到表格中
if (i != dtb.Rows.Count-1)
{
if ((i + 1) % nextPager == 0)
{
PlaceHolder1.Controls.Add(tab);
tab = new Table();
//输出一个换行标记
Panel div1 = new Panel();
div1.CssClass = "NextPage";
PlaceHolder1.Controls.Add(div1);
}
}
else
{
PlaceHolder1.Controls.Add(tab);
}
}
}
}
估计你要自己写一个ocx,调用了
//参考;
<%@ Control Language="c#" AutoEventWireup="false" Inherits="FLX.Portal.PageModeTemplate" %>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style><html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!--media=print 这个属性可以在打印时有效-->
<style media=print>
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style> <style>
.tdp
{
border-bottom: 1 solid #000000;
border-left: 1 solid #000000;
border-right: 0 solid #ffffff;
border-top: 0 solid #ffffff;
}
.tabp
{
border-color: #000000 #000000 #000000 #000000;
border-style: solid;
border-top-width: 2px;
border-right-width: 2px;
border-bottom-width: 1px;
border-left-width: 1px;
}
.NOPRINT {
font-family: "宋体";
font-size: 9pt;
}
</style>
<script>
function QueryString(sName)
{
var sSource = String(window.document.location);
var sReturn = "";
var sQUS = "?";
var sAMP = "&";
var sEQ = "=";
var iPos;
iPos = sSource.indexOf(sQUS);
var strQuery = sSource.substr(iPos, sSource.length - iPos);
var strLCQuery = strQuery.toLowerCase();
var strLCName = sName.toLowerCase();
iPos = strLCQuery.indexOf(sQUS + strLCName + sEQ);
if (iPos == -1)
{
iPos = strLCQuery.indexOf(sAMP + strLCName + sEQ);
if (iPos == -1)
return "";
}
sReturn = strQuery.substr(iPos + sName.length + 2,strQuery.length-(iPos + sName.length + 2));
var iPosAMP = sReturn.indexOf(sAMP);
if (iPosAMP == -1)
return sReturn;
else
{
sReturn = sReturn.substr(0, iPosAMP);
}
return sReturn;
}
var PrintFirst = function(){
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex=1";
}var PrintLast = function(){
var last = QueryString('LastPage');
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+last;
}var PrintPre = function(){
var index = QueryString('PageIndex');
if(index>1)
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+(index-1);
else alert("到头了");
}var PrintNext = function(){
var index = parseInt(QueryString('PageIndex'));
var last = parseInt(QueryString('LastPage'));
if((index+1)<=last)
window.location.href = window.location.href
.replace(/\?PageIndex=(\d)*$/g,"").replace(/\?PageIndex=(\d)*&/g,"?").replace(/&PageIndex=(\d)*&/g,"&").replace(/&PageIndex=(\d)*$/g,"")
+"&PageIndex="+(index+1);
else alert("到头了");
}//创建按钮
var createButton = function(vID,vValue,eventHandler){
var div =document.getElementById("div1");
var btnButton = document.createElement("INPUT");
btnButton.type ="Button";
btnButton.id = vID;
btnButton.value = vValue;
btnButton.onclick = eventHandler;
div.appendChild(btnButton);
}
//创建标签
var createSpan = function(vID,vValue,eventHandler){
var div =document.getElementById("div1");
var htmSpan = document.createElement("span");
htmSpan.id = vID;
htmSpan.innerHTML = vValue;
if(eventHandler != null)
htmSpan.onclick = eventHandler;
div.appendChild(htmSpan);
}
//按钮事件
var print = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,1);
}
var directPrint = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(6,6);
}
var PrintSetUp = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(8,1);
}
var PrintReview = function(){
pagesetup_null();
document.all.WebBrowser.ExecWB(7,1);
}window.onload = function(){
createButton("btn0","打印",print);
createButton("btn1","直接打印",directPrint);
createButton("btn2","页面设置",PrintSetUp);
createButton("btn3","打印预览",PrintReview);
createButton("btn4","首 页",PrintFirst);
createButton("btn5","上一页",PrintPre);
createButton("btn6","下一页",PrintNext);
createButton("btn7","末 页",PrintLast);
var index =QueryString('PageIndex');
var last = QueryString('LastPage');
createSpan("span0"," 当前第 "+index+"/"+last+" 页 ",null);
}
//设置网页打印的页眉页脚为空
function pagesetup_null(){
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER"
hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="header";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
hkey_key="footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
}catch(e){}
}
</script>
</head>
<body >
<center class="Noprint" >
<p>
<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0>
</OBJECT>
<div id="div1">
</div>
</p>
</center>
<div class="page">
<table id="Table_01" width="780" height="360" border="0" cellpadding="0" cellspacing="0" background="行程单.JPG">
<tr><td width="931" style="position:relative; padding:inherit; "><div id="Layer2" align="center" style="position:absolute; left:57px; top:98px; width:55px; height:20px; z-index:2;">FEKPO</div>
<div id="Layer6" style="position:absolute; left:450px; top:72px; width:119px; height:23px; z-index:6; vertical-align:middle" align="center">一星期内有效</div>
<div id="Layer9" style="position:absolute; left:95px; top:276px; width:153px; height:24px; z-index:9;" align="left">7188881131464</div>
<table width="98%" align="center" runat="server" height="" style="position:absolute; top:134px; left: 62px; width: 686px;">
<tr>
<td width="106" align="left" height="20" >北京 FOR </td>
<td width="54" height="20" align="center" >方中信</td>
<td width="40" height="20" align="center" >C10</td>
<td height="20" width="44" align="center" >A级</td>
<td height="20" width="65" align="center" >2008-03-21</td>
<td height="20" width="34" align="center" >09:00</td>
<td height="20" width="147" align="center" >高级/高等</td>
<td height="20" width="57" align="center" >2008-03-21</td>
<td height="20" width="55" align="center" >2008-03-27</td>
<td height="20" width="40" >无任何行李</td>
</tr>
</table>
<div id="Layer10" style="position:absolute; left:98px; top:314px; height:19px; z-index:10; width: 62px;" align="center">086</div>
<div id="layer9" style="position:absolute; left:247px; top:315px; height:18px; z-index:10; width: 260px;" align="center">纵横天地电子商旅公司</div>
<div id="layer" align="center" style="position:absolute; left:684px; top:273px; width:63px; height:24px; z-index:2;">40.00¥</div>
<div id="layer2" align="left" style="position:absolute; left:203px; top:73px; width:203px; height:20px; z-index:2;">43453453</div>
<div id="layer3" style="position:absolute; left:272px; top:281px; height:19px; z-index:10; width: 67px;" align="center">APKS</div>
<div id="layer4" align="center" style="position:absolute; left:57px; top:72px; width:55px; height:20px; z-index:2;">方信</div>
<div id="layer5" align="center" style="position:absolute; left:403px; top:277px; width:55px; height:24px; z-index:2;">无</div>
<div id="layer6" align="center" style="position:absolute; left:643px; top:308px; width:85px; height:24px; z-index:2;">2008-03-21</div>
<table width="98%" align="center" runat="server" height="" style="position:absolute; top:252px; left: 190px; width: 556px; height: 21px;">
<tr>
<td width="102" align="left" height="20" >1000.00¥</td>
<td width="91" height="20" align="center" >90.00¥</td>
<td height="20" width="90" align="center" >70.00¥</td>
<td height="20" width="100" align="center" >40.00¥</td>
<td height="20" width="149" align="center" >1200.00¥</td>
</tr>
</table></td></tr>
</table>
</div> </body>
</html>
也就是这句中的FLX.Portal.PageModeTemplate
这个分页是通过url传参进行分页的,所以只需要在后台显示当前页的数据就行了。
比如如果打印的数据共4页,当前显示第一页:http://www.asd.com/wprint.aspx?pageindex=1&lastpage=4