现在的位置: 首页 > 综合 > 正文

JScript.js实现打印功能

2012年09月06日 ⁄ 综合 ⁄ 共 4149字 ⁄ 字号 评论关闭

//以下脚本实现:打印指定的datagrid

var vGridContent;  //DataGrid的内容
var vHeaderInfo;  //打印的表头
var vTailerInfo;  //打印的表尾

function PrintDataGrid(vDataGrid,title,yyt)
{
  PickupHeaderInfo(title,yyt);
  
  document.body.insertAdjacentHTML("beforeEnd",
    "<iframe name=printHiddenFrame width=0 height=0></iframe>");
 
  var doc = printHiddenFrame.document;
  doc.open();
  doc.write("<body onload=\"setTimeout('parent.onprintHiddenFrame()', 0)\">");
  doc.write("<iframe name=printMe width=0 height=0 ></iframe>");
  doc.write("</body>");
  doc.close();
 
  CreateHtmlReport(printHiddenFrame.printMe,vDataGrid);
  return false;   
}
    
function CreateHtmlReport(vHideFrame,vDataGrid)
{
 vGridContent = vDataGrid.outerHTML;
 
 // 输出报表头信息及抽取过来的表格
  var doc = vHideFrame.document;
 doc.open();
 doc.write("<html><body>");
 doc.write(vHeaderInfo);
 doc.write(vGridContent);
 doc.write("</body></html>");
 doc.close();
 
 
 // 重新设置表格样式
 vDataGrid.borderColor = "#000000";
 vDataGrid.width = "800px";
 vDataGrid.style.fontFamily = "Verdana";
 vDataGrid.style.fontSize = "12px";
 vDataGrid.style.borderRight = "2px solid #000000";
 vDataGrid.style.borderTop = "2px solid #000000";
 vDataGrid.style.borderLeft = "2px solid #000000";
 vDataGrid.style.borderBottom = "2px solid #000000";
 vDataGrid.style.borderCollapse = "collapse";

 // 重新设置表格头样式
 var TBody = vDataGrid.children(0);
 TBody.children(0).style.fontWeight = "bold";
 TBody.children(0).bgColor = "#E7E7E7";
 // 替换原表格底部的页码信息
 var pageInfo = "<td>第 " + ((4 - 3) / 1 + 1) + " 页 / 共 " + "1" + " 页&nbsp;&nbsp;&nbsp;&nbsp;</td>";
}
//创建表头 表尾
function PickupHeaderInfo(title,yyt)
{
 try
 {
  // 提取报表标题字体大小
  var ReportTitleWithSizeInfo = "<font size='" + "+2" + "'>" + title + "</font>"
  var reportDate = "";
  var reportWriter = "";
  var nowdate=new Date();
  reportDate = "<b>统计时间</b>:" +nowdate.toLocaleString() + "<br>";
  reportDate +="<b>营业厅</b>:"+yyt+"<br>"; 
 
  // 生成报表头信息
  vHeaderInfo = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">";
  vHeaderInfo += "<title>"+title+"</title></head>" +
   "<body bgcolor='#FFFFFF' style='color: #000000; font-family: Verdana; font-size:12px; cursor: default'>";
  vHeaderInfo += "<br><p align='center'><b>" + ReportTitleWithSizeInfo + "</b></p>";
  vHeaderInfo += "<p>" + reportDate;
  vHeaderInfo += reportWriter + "</p>";
 }
 catch (e)
 {
  alert("提取报表公共信息失败,打印操作被取消!");
  self.close();
 }
}
//下面的脚本来自msdn
// The code by Captain <cerebrum@iname.com>
    // Mead & Company, http://www.meadroid.com/wpm/

// fake print() for IE4.x
if ( !printIsNativeSupport() )
  window.print = printFrame;

// main stuff
function printFrame(frame, onfinish) {
  if ( !frame ) frame = window;

  if ( frame.document.readyState !== "complete" &&
       !confirm("The document to print is not downloaded yet! Continue with printing?") )
  {
    if ( onfinish ) onfinish();
    return;
  }

  if ( printIsNativeSupport() ) {
    /* focus handling for this scope is IE5Beta workaround,
       should be gone with IE5 RTM.
    */
    var focused = document.activeElement;
    frame.focus();
    frame.self.print();
    if ( onfinish ) onfinish();
    if ( focused && !focused.disabled ) focused.focus();
    return;
  }

  var eventScope = printGetEventScope(frame);
  var focused = document.activeElement;

  window.printHelper = function() {
    execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
    printFireEvent(frame, eventScope, "onafterprint");
    printWB.outerHTML = "";
    if ( onfinish ) onfinish();
    if ( focused && !focused.disabled ) focused.focus();
    window.printHelper = null;
  }

  document.body.insertAdjacentHTML("beforeEnd",
    "<object id=\"printWB\" width=0 height=0 \
    classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");

  printFireEvent(frame, eventScope, "onbeforeprint");
  frame.focus();
  window.printHelper = printHelper;
  setTimeout("window.printHelper()", 0);
}

// helpers
function printIsNativeSupport() {
  var agent = window.navigator.userAgent;
  var i = agent.indexOf("MSIE ")+5;
  return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}

function printFireEvent(frame, obj, name) {
  var handler = obj[name];
  switch ( typeof(handler) ) {
    case "string": frame.execScript(handler); break;
    case "function": handler();
  }
}

function printGetEventScope(frame) {
  var frameset = frame.document.all.tags("FRAMESET");
  if ( frameset.length ) return frameset[0];
  return frame.document.body;
}

function onprintHiddenFrame() {
  function onfinish() {
    printHiddenFrame.outerHTML = "";
    if ( window.onprintcomplete ) window.onprintcomplete();
  }
  printFrame(printHiddenFrame.printMe, onfinish);
}

 

抱歉!评论已关闭.