做web项目时,碰到打印算是比较头疼的事。因项目特点而异,我们可能会采取不同的方式进行。现在我总结一下不同的打印方法:
一. 利用IE浏览器直接打印
1 )打印web页面的所有元素
这是最简单的一种打印,直接调用window.print()即可。代码如下:
1 < button onclick = " javascript:window.print(); " ></ button >
2 )分页打印
在做web分页打印时,我们可以使用css样式表进行控制。css中有个叫page - break - after的属性。page - break - after的说明如下:
1page - break - after版本:CSS2 兼容性:IE4 + 继承性:无
2语法:page - break - after : auto ¦ always ¦ avoid ¦ left ¦ right ¦ null
3参数:
4auto : 假如需要在对象之后插入页分割符
5always : 始终在对象之后插入页分割符
6avoid : 避免在对象后面插入页分割符
7left : 在对象后面插入页分割符直到它到达一个空白的左页边
8right : 在对象后面插入页分割符直到它到达一个空白的右页边
9 null : 空值。IE5用来取消页分割符设置
10
以下是demo的源代码:
这是default2.aspx文件,这里要注意的是第7行有这段话:
< style >
.pagebreak { page-break-after: always }
</ style >
1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default2.aspx.cs " Inherits = " Default2 " %>
2
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4 < html xmlns = " http://www.w3.org/1999/xhtml " >
5 < head runat = " server " >
6 < title > Untitled Page </ title >
7 < style >
8 .pagebreak {} { page-break-after: always }
9 </ style >
10 </ head >
11 < body >
12 < form id = " form1 " runat = " server " >
13 < div >
14 < table id = " TABLE1 " runat = " server " border = " 1 " >
15 < tr >
16 < td style = " width: 100px " >
17 </ td >
18 < td style = " width: 100px " >
19 </ td >
20 < td style = " width: 100px " >
21 </ td >
22 </ tr >
23 </ table >
24 < button onclick = " javascript:window.print(); " > 打 印 </ button >
25
26 </ div >
27 </ form >
28 </ body >
29 </ html >
30
下面是Default2.aspx.cs文件,要注意第43行的设置。
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 public partial class Default2 : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!IsPostBack)
18 {
19 int pageSize = 5; //每页显示元素个数
20 int i = 1;
21
22 SqlConnection conn = new SqlConnection("data source=localhost;uid=sa;pwd=;initial catalog=northwind");
23 conn.Open();
24 SqlDataAdapter adapter = new SqlDataAdapter("select customerid,city,postalcode from customers", conn);
25 DataSet ds = new DataSet();
26 adapter.Fill(ds);
27
28 foreach (DataRow dr in ds.Tables[0].Rows)
29 {
30 i++;
31 HtmlTableRow tr = new HtmlTableRow();
32 HtmlTableCell cell1 = new HtmlTableCell();
33 cell1.InnerHtml = Convert.ToString(dr[0]);
34 HtmlTableCell cell2 = new HtmlTableCell();
35 cell2.InnerHtml = Convert.ToString(dr[1]);
36 HtmlTableCell cell3 = new HtmlTableCell();
37 cell3.InnerHtml = Convert.ToString(dr[2]);
38 tr.Cells.Add(cell1);
39 tr.Cells.Add(cell2);
40 tr.Cells.Add(cell3);
41 if ((i - pageSize) % pageSize == 1)
42 {
43 tr.Attributes["class"]="pagebreak"; //设置打印标签
44 }
45 this.TABLE1.Rows.Add(tr);
46
47 }
48<img src="/Images/OutliningIndicators/ExpandedSubBl%
一. 利用IE浏览器直接打印
1 )打印web页面的所有元素
这是最简单的一种打印,直接调用window.print()即可。代码如下:
1 < button onclick = " javascript:window.print(); " ></ button >
2 )分页打印
在做web分页打印时,我们可以使用css样式表进行控制。css中有个叫page - break - after的属性。page - break - after的说明如下:
1page - break - after版本:CSS2 兼容性:IE4 + 继承性:无
2语法:page - break - after : auto ¦ always ¦ avoid ¦ left ¦ right ¦ null
3参数:
4auto : 假如需要在对象之后插入页分割符
5always : 始终在对象之后插入页分割符
6avoid : 避免在对象后面插入页分割符
7left : 在对象后面插入页分割符直到它到达一个空白的左页边
8right : 在对象后面插入页分割符直到它到达一个空白的右页边
9 null : 空值。IE5用来取消页分割符设置
10
以下是demo的源代码:
这是default2.aspx文件,这里要注意的是第7行有这段话:
< style >
.pagebreak { page-break-after: always }
</ style >
1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default2.aspx.cs " Inherits = " Default2 " %>
2
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4 < html xmlns = " http://www.w3.org/1999/xhtml " >
5 < head runat = " server " >
6 < title > Untitled Page </ title >
7 < style >
8 .pagebreak {} { page-break-after: always }
9 </ style >
10 </ head >
11 < body >
12 < form id = " form1 " runat = " server " >
13 < div >
14 < table id = " TABLE1 " runat = " server " border = " 1 " >
15 < tr >
16 < td style = " width: 100px " >
17 </ td >
18 < td style = " width: 100px " >
19 </ td >
20 < td style = " width: 100px " >
21 </ td >
22 </ tr >
23 </ table >
24 < button onclick = " javascript:window.print(); " > 打 印 </ button >
25
26 </ div >
27 </ form >
28 </ body >
29 </ html >
30
下面是Default2.aspx.cs文件,要注意第43行的设置。
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 public partial class Default2 : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!IsPostBack)
18 {
19 int pageSize = 5; //每页显示元素个数
20 int i = 1;
21
22 SqlConnection conn = new SqlConnection("data source=localhost;uid=sa;pwd=;initial catalog=northwind");
23 conn.Open();
24 SqlDataAdapter adapter = new SqlDataAdapter("select customerid,city,postalcode from customers", conn);
25 DataSet ds = new DataSet();
26 adapter.Fill(ds);
27
28 foreach (DataRow dr in ds.Tables[0].Rows)
29 {
30 i++;
31 HtmlTableRow tr = new HtmlTableRow();
32 HtmlTableCell cell1 = new HtmlTableCell();
33 cell1.InnerHtml = Convert.ToString(dr[0]);
34 HtmlTableCell cell2 = new HtmlTableCell();
35 cell2.InnerHtml = Convert.ToString(dr[1]);
36 HtmlTableCell cell3 = new HtmlTableCell();
37 cell3.InnerHtml = Convert.ToString(dr[2]);
38 tr.Cells.Add(cell1);
39 tr.Cells.Add(cell2);
40 tr.Cells.Add(cell3);
41 if ((i - pageSize) % pageSize == 1)
42 {
43 tr.Attributes["class"]="pagebreak"; //设置打印标签
44 }
45 this.TABLE1.Rows.Add(tr);
46
47 }
48<img src="/Images/OutliningIndicators/ExpandedSubBl%