1using System;
2using System.IO;
3using System.Collections;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.Web;
8using System.Web.SessionState;
9using System.Web.UI;
10using System.Web.UI.WebControls;
11using System.Web.UI.HtmlControls;
12using System.Text;
13
14
15namespace xxxx
16{
17 /// <summary>
18 /// clsPrint 的摘要说明。
19 /// </summary>
20 public class CSPrint
21 {
22 public CSPrint()
23 {
24 //
25 // TODO: 在此处添加构造函数逻辑
26 //
27 }
28
29 //打印DATAGRID数据
30 public void dataGridPrint()
31 {
32
33 }
34
35 public string pagePrint(DataTable dt,string tableName)
36 {
37 //pagePrint:根据DataTable转换成对应的HTML对应的字符串并打印
38 DataTable myDataTable=new DataTable();
39 myDataTable=dt;
40
41 int myRow=myDataTable.Rows.Count;
42 int myCol=myDataTable.Columns.Count;
43
44//修改列标题
45 myDataTable.Columns[0].ColumnName = "x1";
46 myDataTable.Columns[1].ColumnName = "x2";
47 myDataTable.Columns[2].ColumnName = "x3";
48 myDataTable.Columns[3].ColumnName = "x4";
49 myDataTable.Columns[4].ColumnName = "x5";
50 myDataTable.Columns[5].ColumnName = "x6";
51 myDataTable.Columns[6].ColumnName = "x7";
52
53
54
55
56 StringBuilder sb=new StringBuilder();
57
58 string colHeaders="<html>" + "<head><title>" + tableName + "</title>"
59 + "</head>"
60 + "<body>"
61 + "<script language='javascript'>"
62 + " try"
63 + " {"
64 + " var Wsh=new ActiveXObject('WScript.Shell');"
65 + " HKEY_Key='header';"
66 + " Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,'');"
67 + " HKEY_Key='footer';"
68 + " Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,'');"
69 + " }"
70 + " catch(e){}</script>"
71 + "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"
72 + "<table border=1 cellspacing=0 cellpadding=0><caption align=center><font size=5><B>" + tableName + "</B></font></caption><tr>";
73
74 for(int i=0;i<myCol;i++)
75 {
76 colHeaders +="<th align=center>"+ myDataTable.Columns[i].ColumnName.ToString()+"</th>";
77 }
78 colHeaders += "</tr>";
79 sb.Append(colHeaders);
80
81 for(int i=0;i<myRow;i++)
82 {
83 sb.Append("<tr>");
84 for(int j=0;j<myCol;j++)
85 {
86 sb.Append("<td align=center>");
87 if (myDataTable.Rows[i][j].ToString().Trim() != "")
88 {
89 sb.Append(myDataTable.Rows[i][j].ToString().Trim());
90 }
91 else
92 {
93 sb.Append(" ");
94 }
95 sb.Append("</td>");
96 }
97 sb.Append("</tr>");
98 }
99
100 sb.Append("</table></body></html>");
101 colHeaders=sb.ToString();
102 colHeaders+="<script languge='javascript'>window.opener=null;WebBrowser.ExecWB(6,1); window.close();</script>";
103 return(colHeaders);
104
105 }
106 }
107}
2using System.IO;
3using System.Collections;
4using System.ComponentModel;
5using System.Data;
6using System.Drawing;
7using System.Web;
8using System.Web.SessionState;
9using System.Web.UI;
10using System.Web.UI.WebControls;
11using System.Web.UI.HtmlControls;
12using System.Text;
13
14
15namespace xxxx
16{
17 /// <summary>
18 /// clsPrint 的摘要说明。
19 /// </summary>
20 public class CSPrint
21 {
22 public CSPrint()
23 {
24 //
25 // TODO: 在此处添加构造函数逻辑
26 //
27 }
28
29 //打印DATAGRID数据
30 public void dataGridPrint()
31 {
32
33 }
34
35 public string pagePrint(DataTable dt,string tableName)
36 {
37 //pagePrint:根据DataTable转换成对应的HTML对应的字符串并打印
38 DataTable myDataTable=new DataTable();
39 myDataTable=dt;
40
41 int myRow=myDataTable.Rows.Count;
42 int myCol=myDataTable.Columns.Count;
43
44//修改列标题
45 myDataTable.Columns[0].ColumnName = "x1";
46 myDataTable.Columns[1].ColumnName = "x2";
47 myDataTable.Columns[2].ColumnName = "x3";
48 myDataTable.Columns[3].ColumnName = "x4";
49 myDataTable.Columns[4].ColumnName = "x5";
50 myDataTable.Columns[5].ColumnName = "x6";
51 myDataTable.Columns[6].ColumnName = "x7";
52
53
54
55
56 StringBuilder sb=new StringBuilder();
57
58 string colHeaders="<html>" + "<head><title>" + tableName + "</title>"
59 + "</head>"
60 + "<body>"
61 + "<script language='javascript'>"
62 + " try"
63 + " {"
64 + " var Wsh=new ActiveXObject('WScript.Shell');"
65 + " HKEY_Key='header';"
66 + " Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,'');"
67 + " HKEY_Key='footer';"
68 + " Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,'');"
69 + " }"
70 + " catch(e){}</script>"
71 + "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"
72 + "<table border=1 cellspacing=0 cellpadding=0><caption align=center><font size=5><B>" + tableName + "</B></font></caption><tr>";
73
74 for(int i=0;i<myCol;i++)
75 {
76 colHeaders +="<th align=center>"+ myDataTable.Columns[i].ColumnName.ToString()+"</th>";
77 }
78 colHeaders += "</tr>";
79 sb.Append(colHeaders);
80
81 for(int i=0;i<myRow;i++)
82 {
83 sb.Append("<tr>");
84 for(int j=0;j<myCol;j++)
85 {
86 sb.Append("<td align=center>");
87 if (myDataTable.Rows[i][j].ToString().Trim() != "")
88 {
89 sb.Append(myDataTable.Rows[i][j].ToString().Trim());
90 }
91 else
92 {
93 sb.Append(" ");
94 }
95 sb.Append("</td>");
96 }
97 sb.Append("</tr>");
98 }
99
100 sb.Append("</table></body></html>");
101 colHeaders=sb.ToString();
102 colHeaders+="<script languge='javascript'>window.opener=null;WebBrowser.ExecWB(6,1); window.close();</script>";
103 return(colHeaders);
104
105 }
106 }
107}