往往要将一个表格内容打印出来,这也是比较烦的,表格可能含有文本框或其他控件,如何实现在客户端打印呢?我还是用了和导入时相似的方法,生成一个网页文件,在其中生成一个普通的表格(将网页上的表格先转成一般的HTML表格),并可以在前面加一个标题以及在后面加页脚。并在此网页的onload()事件中添加打印代码,可实现预览或打印,并可指定每页打印多少行。
C#代码,生成一个HTML文件。
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.Web.UI;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebPrinter
{
///
/// WebPrinter 的摘要说明。
///
public class WebPrint
{
private DataGrid dgPrintGrid;
private int nHeaderLines;
private int nLinesPerPage;
private string sTitle, sBottomTitle;
private int opCode;
private string sUserPage;
private string sPath;
public WebPrint()
{
}
#region 设置要打印的表格
///
/// 设置要打印的表格
///
public DataGrid Grid
{
get{return dgPrintGrid;}
set{dgPrintGrid = value;}
}
#endregion
#region 设置要将表格开头的多少行作为标题行
///
/// 设置要将表格开头的多少行作为标题行
///
public int HeaderLines
{
get{return nHeaderLines;}
set{nHeaderLines = value;}
}
#endregion
#region 设置每页打印多少行,为0时表示不限.
///
/// 设置每页打印多少行,为0时表示不限.
///
public int LinesPerPage
{
get{return nLinesPerPage;}
set{nLinesPerPage = value;}
}
#endregion
#region 设置打印的标题,为""时不打印标题.
///
/// 设置打印的标题,为""时不打印标题.
///
public string PrintTitle
{
get{return sTitle;}
set{sTitle = value;}
}
#endregion
#region 设置打印的底部标题,为""时不打印标题.
///
/// 设置打印的底部标题,为""时不打印标题.
///
public string BottomTitle
{
get{return sBottomTitle;}
set{sBottomTitle = value;}
}
#endregion
#region 操作代码:0页面设置,1预览,2打印.
///
/// 操作代码:0页面设置,1预览,2打印.
///
public int OperateCode
{
get{return opCode;}
set{opCode = value;}
}
#endregion
#region 当前用户的打印文件名称,可用该用户的IP地址或用户名.
///
/// 当前用户的打印文件名称,可用该用户的IP地址或用户名.
///
public string UserPage
{
get{return sUserPage;}
set{sUserPage = value;}
}
#endregion
#region 主目录路径
///
/// 主目录路径
///
public string Path
{
get{return sPath;}
set{sPath = value;}
}
#endregion
#region 根据Grid生成表格内容.
///
/// 根据Grid生成表格内容.
///
///
private string getTable()
{
string sTable="";
string sTemp="", sTemp2;
string sCssName = dgPrintGrid.CssClass;
string sLF = System.Environment.NewLine;
//表格
sTable = sTable + "
" + sLF;
//标题
if (sTitle != "")
{
sTable = sTable + "" + sLF;
sTable = sTable + ""+sTitle+" | " + sLF;
sTable = sTable + "
" + sLF;
}
//主内容
sTable = sTable + "" + sLF;
sTable = sTable + "" + sLF; sTable = sTable + "" + sLF; //表头 sTable = sTable + "" + sLF; sTable = sTable + ""; sTemp2 = dgPrintGrid.HeaderStyle.HorizontalAlign.ToString(); for(int j=0; j if (dgPrintGrid.Columns[j].Visible) { sTable = sTable + ""+dgPrintGrid.Columns[j].HeaderText+" | "; } sTable = sTable + " " + sLF; for(int i=0; i { sTable = sTable + ""; for(int j=0; j if (dgPrintGrid.Columns[j].Visible) sTable = sTable + ""+dgPrintGrid.Items[1+i].Cells[j].Text+" | "; sTable = sTable + " " + sLF; } sTable = sTable + "" + sLF;
//行内容 sTemp2 = dgPrintGrid.ItemStyle.HorizontalAlign.ToString(); int nLines = 0; for(int j=HeaderLines-1; j { if (nLinesPerPage!=0 && nLines!=0 && nLines%nLinesPerPage == 0) sTable = sTable + ""; else sTable = sTable + " "; for(int i=0; i if (dgPrintGrid.Columns[i].Visible) { if(dgPrintGrid.Columns[i] is System.Web.UI.WebControls.TemplateColumn) { //要增加对其他控件的支持可修改这里。 if (dgPrintGrid.Items[j].Cells[i].Controls[1] is System.Web.UI.WebControls.TextBox) sTemp = ((System.Web.UI.WebControls.TextBox)dgPrintGrid.Items[j].Cells[i].Controls[1]).Text; else if (dgPrintGrid.Items[j].Cells[i].Controls[1] is System.Web.UI.WebControls.Label) sTemp = ((System.Web.UI.WebControls.Label)dgPrintGrid.Items[j].Cells[i].Controls[1]).Text; else if (dgPrintGrid.Items[j].Cells[i].Controls[1] is System.Web.UI.WebControls.DropDownList) sTemp = ((System.Web.UI.WebControls.DropDownList)dgPrintGrid.Items[j].Cells[i].Controls[1]).SelectedItem.Text; } else if (dgPrintGrid.Columns[i] is System.Web.UI.WebControls.BoundColumn) sTemp = dgPrintGrid.Items[j].Cells[i].Text; sTable = sTable + ""+sTemp+" | "; } sTable = sTable + " " + sLF; nLines++; }
//尾部 sTable = sTable + " " + sLF; sTable = sTable + " | " + sLF;
sTable = sTable + "
" + sLF;
//底部标题
if (sBottomTitle != "")
{
sTable = sTable + "" + sLF;
sTable = sTable + ""+sBottomTitle+" | " + sLF;
sTable = sTable + "
" + sLF;
}
sTable = sTable + "
" + sLF;
//选择操作
sTable = sTable + "" + sLF;
return sTable;
}
#endregion
#region 打印表格.
///
/// 打印表格.
///
///
public void PrintGrid(System.Web.HttpResponse Response)
{
StringBuilder htmltext=new StringBuilder();
try
{
if(!File.Exists(sPath + "\\PrintFile\\" + sUserPage))
{
FileStream fs = File.Create(sPath + "\\PrintFile\\" + sUserPage);
fs.Close();
}
//读出模板内容
using (StreamReader sr = new StreamReader(sPath + "\\PrintTemp.htm"))
{
string sLine = "";
while ((sLine = sr.ReadLine()) != null)
{
htmltext.Append(sLine);
}
sr.Close();
}
//替换模板的主体内容
htmltext.Replace("PrintTempBody", getTable());
//生成打印文件并打开.
using (StreamWriter sw = new StreamWriter(sPath + "\\PrintFile\\"+sUserPage,false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
Response.Write("");
}
}
catch(Exception ex)
{
Response.Write("");
}
}
#endregion
}
}
在生成的HTML文件中要用到的javascript代码:
var hkey_root,hkey_path,hkey_key
hkey_root="HKEY_CURRENT_USER"
hkey_path="\Software\Microsoft\Internet Explorer\PageSetup"
//设置网页打印的页眉页脚为空
function pagesetup_null()
{
try
{
var RegWsh = CreateObject("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)
{
//alert("您的浏览器不支持此功能");
}
}
//预览
function prepare()
{
try
{
//top.MainFrame.focus();
WB.ExecWB(7,1)
}
catch(e)
{
//alert("您的浏览器不支持此功能")
}
}
//打印预览
function preView()
{
pagesetup_null();
prepare();
}
//页面设置
function pageSetup()
{
try{WB.ExecWB(8,1)}catch(e){alert("您的浏览器不支持此功能")}
}
//打印
function print()
{
try{WB.ExecWB(6,6)}catch(e){alert("您的浏览器不支持此功能")}
}
调用例子:
WebPrint pr = new WebPrint();
pr.Grid = DataGrid1;
pr.HeaderLines = nHeaderLines;
pr.LinesPerPage = nLinesPerPage;
pr.OperateCode = 2;
pr.Path = sPath;
pr.PrintTitle = sTitle;
pr.BottomTitle = sBottomTitle;
pr.UserPage = sUserPage;
pr.PrintGrid(Response);
本文作者:佚名 来源:http://blog.sina.com.cn/s/blog_5447ea43010006am.ht
CIO之家 www.ciozj.com 微信公众号:imciow