当前位置:编程学习 > C#/ASP.NET >>

IE直接打印

////以下脚本实现:打印指定的datagrid
var vGridContent;  //DataGrid的内容
var vHeaderInfo;  //打印的表头
var vTailerInfo;  //打印的表尾

/*
目的:在页面写入隐藏帧并打印
参数:
vDataGrid  所要打印的DataGrid句柄

备注:
 代码中调用如下
 btPrint.Attributes.Add("onclick","return PrintDataGrid(document.all('SheetList'))");
 
 SheetList为待打印的DataGrid的ID
*/
function PrintDataGrid(vDataGrid)
{
  PickupHeaderInfo();
  
  document.body.insertAdjacentHTML("beforeEnd",
    "<iframe name=printHiddenFrame width=0 height=0></iframe>");
问题一:“insertAdjacentHTML”是个什么函数?起了怎样的作用???

 
  var doc = printHiddenFrame.document;
  doc.open();
  doc.write("<body onload=\"setTimeout('parent.onprintHiddenFrame()', 0)\">");
问题二:“<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;    
}

/*
目的:在隐藏帧中写入DataGrid的内容,并重写DataGrid的格式
参数:
vHideFrame 隐藏帧的句柄
vDataGrid  所要打印的DataGrid句柄

备注:
*/
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 = "100%";
 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" + " 页    </td>";
}

//创建表头 表尾
function PickupHeaderInfo()
{
 try
 {
  // 提取报表标题字体大小
  var ReportTitleWithSizeInfo = "<font size='" + "+2" + "'>" + "无费用用户统计" + "</font>"
  var reportDate = "";
  var reportWriter = "";
  var nowdate=new Date();
  reportDate = "<b>统计时间</b>:" +nowdate.toLocaleString() + "<br>";
  reportDate +="<b>营业厅</b>:测试而已<br>";  
 
  // 生成报表头信息
  vHeaderInfo = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">";
  vHeaderInfo += "<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);
}

问题五:
  程序中在Page_Load里面加上:btPrint.Attributes.Add("onclick","return PrintDataGrid(document.all('SheetList'))");
        注:SheetList为需要打印的DataGrid ID,在查询后,btPrint为页面上打印按钮的ID
要打印Table控件,是不是只要只要把DataGrid控件的ID换为Table控件的ID就可以了呢??? --------------------编程问答-------------------- 大侠呢???帮忙看看啊。。。 --------------------编程问答-------------------- --------------------编程问答-------------------- 头晕中..... --------------------编程问答-------------------- parent.onprintHiddenFrame()

vGridContent = vDataGrid.outerHTML获取输出的内容。


打印


一、WebBrowser控件

<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>

二、WebBrowder控件的方法

//打印

WebBrowser1.ExecWB(6,1);

//打印设置

WebBrowser1.ExecWB(8,1);

//打印预览

WebBrowser1.ExecWB(7,1);

三、实现打印的设置,打印数据的生成,打印的预览,和打印。

实现打印的设置,打印数据的生成,打印的预览,和打印我一般这样做,假如查询结果在a.asp中,那么在a.asp中放置打印设置、打印预览、打印三个按钮。

单击打印设置按钮则在js中执行WebBrowser1.ExecWB(8,1),以打开打印设置窗口。

单击打印预览按钮则打开一个b.asp,在b.asp中重新生成打印数据,然后在b.asp中自动执行WebBrowser1.ExecWB(7,1),以打开用户预览界面。

单击打印按钮则也打开b.asp,在b.asp中重新生成打印数据,然后在b.asp中自动执行WebBrowser1.ExecWB(6,1),以自动打印数据。

四、代码

a.asp调用数据的程序就不给出了。只给出几个按钮的代码:

<input type=“button“ name=“mPrint“ value=“打印“ onclick=“exePrint();“>

<input type=“button“ name=“mPreview“ value=“打印预览“ onclick=“exePreview();“>

<input type=“button“ name=“mSetting“ value=“打印设置“ onclick=“exeSetting();“>


--------------------编程问答--------------------
引用 4 楼 wxr0323 的回复:
parent.onprintHiddenFrame()

vGridContent = vDataGrid.outerHTML获取输出的内容。


打印


一、WebBrowser控件

<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'>……


给力,今天晚上研究研究。。。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,