JS获取页面数据并打印

2024-03-17 00:58

本文主要是介绍JS获取页面数据并打印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<%@ Page Language="C#" %>
<script runat="server">
    private struct ImageItem {
        public string 图片路径;
        public string 款号;
        public string 系列号;
    }   
    protected void Page_Load(object sender, EventArgs e)
    {
        string ptype = "";
        if (Request.QueryString["ptype"]!=null) {
            ptype = Request.QueryString["ptype"];
        }
        if (Request.Form["ptype"]!= null)
        {
            ptype = Request.Form["ptype"];
        }  
        switch (ptype) {
            case "":    //生成打印脚本
                Response.Write(code1.Value);
                break;
            case "1":   //生成打印页
                Response.Write("<html>");
                Response.Write(code2.Value);
                Response.Write("</html>");
                break;
            case "2":   //返回空页面
                Response.Write("<html>");
                Response.Write("</html>");
                break;
            case "3":  //获取成品图片
                GetImageList("1");
                break;
            case "4":  //获取物料图片
                GetImageList("2");
                break;
        }
      
        Response.End();
        
    }
    private ImageItem GetImageUrl(string idstr, string ylcls)
    {
        ImageItem imgitm ;
        imgitm.款号 = "";
        imgitm.图片路径 = "";
        imgitm.系列号 = "";  
       
        string sql = "";
        if (idstr.Trim().Length == 0) {
            return imgitm;
        }
        if (ylcls == "1") {// 1为成品 
            sql = "SELECT jb_bag.kp_images,jb_bag.kp_item_no, jb_bag.kp_item_code, jb_series.kp_series_name"+
                  "FROM jb_bag INNER JOIN jb_series ON jb_bag.kp_series=jb_series.kp_series_id and kp_item_no="+idstr;
               
        }
        else{
            sql="select kp_url from jb_bagyl where kp_item_no="+idstr;
        }   
        //return "http://t2.baidu.com/it/u=2107939524,1661719057&fm=3&gp=1.jpg";
        System.Data.DataTable db = GenericDataAccess.selectcommand(GenericDataAccess.createcommand(sql));
        if (db.Rows.Count > 0)
        {
            imgitm.图片路径 = "../ProImage/" + db.Rows[0].ItemArray[0].ToString();
            if (db.Rows[0].ItemArray.Length > 1) {
                imgitm.款号 = db.Rows[0].ItemArray[2].ToString();
                imgitm.系列号 = db.Rows[0].ItemArray[3].ToString();       
            }
        }
        return imgitm;
    }
    private void GetImageList(String ylcls)
    {
        string ids = "";
        string htm = "";
        ImageItem im;
        int ii = 0;//换行标记
        int iii = 0; //换页标记
        if (this.Request["ids"] != null) {
            ids = this.Request["ids"];
        }
        string[] idarray = ids.Split("`".ToCharArray());
        if (ylcls == "1")
        {
            for (int i = 0; i < idarray.Length - 1; i++)
            {
                if (ii == 0)
                {
                    if (iii == 0)
                    { //换页
                        htm = htm + "<table>/n";
                    }
                    iii = iii + 1;
                    htm = htm + "   <tr>/n";
                }
                ii = ii + 1;
                im = GetImageUrl(idarray[i], ylcls);
                htm = htm + "       <td><img class='hb' src='" + im.图片路径  + "' class='pic'><br><span>" + im.系列号  + "/" + im.款号  + "</span></td>/n";
                if (ii == 5)
                {
                    ii = 0;
                    htm = htm + "   </tr>/n";
                    if (iii == 5)
                    {
                        htm = htm + "</table>`/n";
                        iii = 0;
                    }
                }

            }
            if (ii != 0)
            {
                htm = htm + "   </tr>/n";
            }
            if (iii != 0)
            {
                htm = htm + "   </table>`/n";
            }
        }
        else
        {
            for (int i = 0; i < idarray.Length - 1; i++)
            {
                htm = htm + GetImageUrl(idarray[i], ylcls).图片路径 + "`";

            }
        }
        Response.Write(htm); 
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <textarea id="code1" cols="20" rows="2" runat='server'> 
String.prototype.Trim = function(){ return Trim(this);}
String.prototype.LTrim = function(){return LTrim(this);}
String.prototype.RTrim = function(){return RTrim(this);}

//此处为独立函数
function LTrim(str)
{
    var i;
    for(i=0;i<str.length;i++)
    {
        if(str.charAt(i)!=" "&&str.charAt(i)!=" ")break;
    }
    str=str.substring(i,str.length);
    return str;
}
function RTrim(str)
{
    var i;
    for(i=str.length-1;i>=0;i--)
    {
        if(str.charAt(i)!=" "&&str.charAt(i)!=" ")break;
    }
    str=str.substring(0,i+1);
    return str;
}
function Trim(str)
{
    return LTrim(RTrim(str));
}
       
             
//==============================以上为字符串处理函数==============================================     

function initXMLHTTP(){ //初始化xmlhttp对象
           if (window.ActiveXObject && !window.XMLHttpRequest)
           {
               window.XMLHttpRequest = function()
               {
                var MSXML = ['Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP'];
                for (var i = 0; i < MSXML.length; i++)
                {
                 try { 
                     return new ActiveXObject(MSXML[i]);
                  }
                 catch (e){}
                }
                return null;
               };
            }
}
initXMLHTTP();
function GetTextByNet(sender){
        var s = "";
        var xhttp = new XMLHttpRequest()
        sender=sender.replace(/`/g,"%60").replace(//|/,"%7C")
        try {
            xhttp.open("Post","../JScript/printer.aspx",false)
            xhttp.setRequestHeader("Content-Type",   "text/xml;charset=utf-8");  
            xhttp.setRequestHeader("Content-Length",sender.length);
            xhttp.send(sender);
            var s = xhttp.responseText;
            xhttp = null;
       }
       catch(e){
            xhttp.open("get","../JScript/printer.aspx?" + sender,false)
            xhttp.send(null);
            var s = xhttp.responseText;
            xhttp = null;
       }
       xhttp = null;
       return s;
}
//================================以上为初始化xmlhttp对象================================================
     
       
var  win = null;        //对话框的window对象
var  testdiv = null     //用于获取数据后存放的容器,以便检测数据表格的高度和宽度等,便于分页             
document.οnclick=function()
{
    var tmpobj = event.srcElement
    if(tmpobj.tagName== "INPUT")
    {
        if(tmpobj.value.replace(//s/g,"")=="打印")
        {
           
            printbutton=tmpobj;
            cviewpage();
            printbutton.οncοntextmenu=function (){return false;}
            return false;
           
        }
      
    }
    function cviewpage(){
        var t = new Date
        window.blur();
        win= window.open("../JScript/printer.aspx?ptype=1&t=" + t.getTime(),"_blank","toolbar=no,menubar=no,status=1,resizable=1,scroll=0,Left=0,Top=0,Width=" + (screen.width - 10)+",Height=" + (screen.Height-65))
    }
}

 

var myTitle = ""
var myAddress = ""
var myPhone = "";
var myFax= "";
function getheaddata() {
    var tab= document.all["editt"];
    var i=0,ii=0;iii=0;
    var tit = getdocumenttitle().replace("查询","").replace("管理","");
    try{
        report_load()
        if(myTitle.length>0){
            tit=myTitle;
        }
    }
    catch(e){
   
    }
    var addrbar = ""
    var tbstr = "<table style='width:100%' ><tr><td colspan=6 style='text-align:center;font-size:18px;font-weight:bold'>" + tit + "</td></tr></table>";
    if(myAddress.length >0) {
        addrbar = addrbar + "&nbsp;&nbsp;地址:" + myAddress + "&nbsp;&nbsp;&nbsp;&nbsp;"
    }
    if(myPhone.length >0) {
        addrbar = addrbar + "&nbsp;&nbsp;电话:" + myPhone + "&nbsp;&nbsp;&nbsp;&nbsp;"
    }
    if(myFax.length >0) {
        addrbar = addrbar + "&nbsp;&nbsp;传真:" + myFax + "&nbsp;&nbsp;&nbsp;&nbsp;"
    }
    tbstr = tbstr + "<table style='width:100%' ><tr><td colspan=6 style='font-size:14px;padding-left:30px'>" + addrbar + "</td></tr></table>";
    tbstr = tbstr + "<table id='tabHead'  style='font-size:14px;width:100%;text-align:left;border-collapse:collapse'>"
    var rindex=0;
    var iiobj;
    if(tab){
        if(document.all["sheetnospan"]){
            tbstr =tbstr + "<tr><td class='lhd' ><pre>单号:</td><td class='rhd'><pre>" + document.all["sheetnospan"].all[0].value.Trim()  + "</td>" 
            rindex=rindex+2;
        }
        for(i=0;i<tab.rows.length;i++){
            for(ii=0;ii<tab.rows[i].cells.length;ii++){
                td=tab.rows[i].cells[ii]
                if(td.all.length==0){
                    if(delspacechar(td.innerText).length>0){
                        if(rindex==0){
                            tbstr =tbstr + "<tr>"
                        }
                        if(td.innerText.replace(":",":").indexOf(":")>-1){
                            tbstr =tbstr + "<td  class='lhd'><pre>" + td.innerText.Trim() + "</td>"
                        }
                        else{
                            tbstr =tbstr + "<td  class='rhd'><pre>" + td.innerText.Trim()  + "</td>"
                        }
                        rindex=rindex+1;
                        if(rindex==6){
                            tbstr =tbstr+"</tr>"
                            rindex=0;
                        }
                       
                    }
                }
                else{
                    if(td.all[0].tagName=="INPUT"){
                        if(rindex==0){
                            tbstr =tbstr + "<tr>"
                        }
                        tbstr =tbstr  + "<td class='rhd'><pre>" + td.all[0].value.Trim()  + "</td>"
                        rindex=rindex+1;
                        if(rindex==6){
                            tbstr =tbstr + "</tr>"
                            rindex=0;
                        }
                    }else if(td.all[0].tagName=="SELECT"){
                        if(rindex==0){
                            tbstr =tbstr + "<tr>"
                        }
                        tbstr =tbstr  + "<td class='rhd'><pre>" + td.all[0].options[td.all[0].selectedIndex].text.Trim()  + "</td>";
                        rindex=rindex+1;
                        if(rindex==6){
                            tbstr =tbstr + "</tr>"
                            rindex=0;
                        }
                    }
                }
            }
        }
       
        if((rindex-1)%6!=0&&rindex>0){
            tbstr =tbstr + "</tr>";
        }
    }
    else{
        rindex=0
        ns=""
        tab=document.getElementById("TABLE1")
        if(tab){
            var mdiv = document.createElement("form");
            mdiv.innerHTML=tab.outerHTML;
            var sheetno = tab.rows[2].cells[0].innerText.Trim()
            if(sheetno.Trim().length<10){
                if(document.all["sheetnospan"]){
                    sheetno=document.all["sheetnospan"].innerText;
                }
            }
            tbstr =tbstr + "<tr><td class='lhd' ><pre>单号:</td><td class='rhd'><pre>" + sheetno + "</td>" 
            rindex=rindex+2;
           
            var elements = mdiv.getElementsByTagName('input');
            RemoveControl(elements);
            elements = mdiv.getElementsByTagName('textarea');
            RemoveControl(elements);
            elements = mdiv.getElementsByTagName('select');
            RemoveControl(elements);
            RemoveGriw(mdiv)    //移除gridview
            RemoveRequiredField(mdiv)  //移除!号
            tbstr=tbstr + getheaddata2(mdiv.innerText,sheetno)
           
        }
        
    }
    tbstr = tbstr + "</table>";
    return tbstr;
}
function RemoveRequiredField(mobj){
    var i = 0,itemobj
    for(i=0;i<mobj.all.length;i++){
           itemobj=mobj.all[i]
           try{
                   if(itemobj.style.visibility.Trim().toLowerCase()=="hidden"){
                       itemobj.innerHTML="";
                   }
                   if(itemobj.display.Trim().toLowerCase()=="none"){
                       itemobj.innerHTML="";
                   }
           }
           catch(e){
          
           }
    }
}
function removeHiddenObj(){

}
function getheaddata2(txt,sheetno){
    var ldat = new Array();
    var lldat = new Array();
    var htm ="";
    var rindex=2;
    var ntxt=txt.replace(//n/g,"`").replace(//s/g,"`").replace(/```/g,"`").replace(/``/g,"`")
    ntxt=ntxt.replace(/:/g,":").replace(/:/g,":`").replace(/```/g,"`").replace(/``/g,"`")
    var cells = ntxt.split("`");
    var i,ii,hs=false;
    for(i=0;i<cells.length;i++){
   
            if(cells[i].indexOf(":")>-1){
                ldat[ldat.length]=cells[i];
            }
            else{
                if(!(cells[i]==sheetno&&i<2) && cells[i].length>0 ){
                    if(ldat.length>0){
                        hs=false
                       
                        for(ii=0;ii<ldat.length;ii++){
                            if(ldat[ii].indexOf(":")>-1&&!hs){
                                  if(ldat[ii].indexOf("·")==-1){ 
                                        ldat[ii]=ldat[ii]+ "·" + cells[i];
                                        hs=true
                                  }
                                
                            }
                        }
                       
                        if(!hs){  //无则新建 
                            if(ldat[ldat.length-1].indexOf(":")>-1){ 
                                        ldat[ldat.length-1]=ldat[ldat.length-1]+ "-" + cells[i];
                                        hs=true
                            }
                            else{
                                ldat[ldat.length-1]=ldat[ldat.length-1]+ "·" + cells[i];
                            }
                        }
                       
                       
                    }
                }
            }
           
    }
    for(i=0;i<ldat.length;i++){
        lldat = ldat[i].split("·")
        if(rindex==0){
                htm =  htm + "<tr>"
        }
        htm=htm + "<td class='lhd'><pre>" + lldat[0] + "</td>"
        if(lldat.length>1){
            for(ii=2;ii<lldat.length-1;ii++){
                lldat[1]=lldat[1] + "·" + lldat[ii];
            }
            htm=htm + "<td  class='rhd'>" + lldat[1] + "</td>"
        }
        else{
            htm=htm+ "<td>无</td>"
        }
        rindex=rindex+2
        if(rindex==6){
                 htm =htm + "</tr>"
                 rindex=0;
        }
    }
    return htm;
}
function RemoveGriw(frm){
    var i=0;
    var itm;
    var ii=0;
    var gvw;
    for(i=0;i<frm.all.length;i++){
        itm=frm.all[i];
        if(itm.tagName=="TR"){
            if(itm.className.replace(" ","")=="GridViewHeaderStyle"){
                if(itm.style.display.toLowerCase()!="none"&&itm.style.visibility.toLowerCase()!="hidden"){
                    var gvw= itm.parentElement;
                    for(ii=itm.parentElement.rows.length-1;ii>-1;ii--){
                       gvw.deleteRow(ii)
                    }
                }
            }
        }
    }
}
function RemoveControl(elements)
 {
  var arrObj = new Array();
 
  var count = elements.length;
  for(var i=0;i<count;i++)
  {
   if(elements[i] == undefined)
    continue;
   
   var obj = document.createElement('span');
   switch(elements[i].type)
   {
    case "text" :
      if(elements[i].value.Trim().length>0){
        obj.setAttribute("innerHTML","&nbsp;" + elements[i].value+ "&nbsp;");
        }
      else{
        obj.setAttribute("innerHTML","&nbsp;无&nbsp;");
      }
       
      break;
    case "textarea" :
      obj.setAttribute("innerHTML","&nbsp;" + elements[i].innerHTML + "&nbsp;");
      elements[i].innerHTML = '';
      break;
    case "select-one" :
      for(var j=0;j<elements[i].length;j++)
      {
       if(elements[i][j].selected)
       {
        obj.setAttribute("innerHTML","&nbsp;" + elements[i][j].text + "&nbsp;");
        break;
       }
      }
      elements[i].options.length = 0;
      break;     
   }
   elements[i].parentNode.appendChild(obj);
   arrObj[arrObj.length] = elements[i];
  }
 
  //删除表单原控件
  for(var i=0;i<arrObj.length;i++)
  {
   arrObj[i].removeNode();
  }
 }

var SplitPrintData_row = 0;

function rootclass(){
    var obj = new Object()
    var currIE = null;
    var br =null;
    var imagediv = null;
    obj.title=function (){
        return getdocumenttitle();
    }
    obj.win=function() {
        return self;
    }
    obj.loaddata = function(iebg,pSize,ylImage){  //加载打印数据 ylImage为物料图像字段
        var headHeight = 60;
        var hdtab = getheaddata();
        var addHeight = 0;
        var pageCount = 0;
        var i = 0 ;
        var icc = 0 ;
        var css ="<style type='text/css'>img.hb{}td.tjcell{font-size:14px;text-align:center;padding:10}td.rhd{padding:0 5 0 3;font-size:14px}td.lhd{text-align:right;padding-right:0px;width:14% }body{overflow:hidden;border:0px}td.data{font-size:13px;color:#000000;border:1px solid #000000; padding-Left:5px;height:26}.pageTop{position:absolute;top:0;left:0;width:100%;font-size:12px}</style>"
        if(!testdiv){
             ctestdiv();
        }
       
        testdiv.innerHTML=createprintdata(ylImage);
        if(!win)
        {
            alert("页面打印预览页面正在加载...")
            return;
       }
        pageCount = parseInt(testdiv.all[0].offsetHeight / pSize)   //获取分页页数,一页数据定义为800
        if(testdiv.all[0].offsetHeight  -pSize*pageCount>0){
            pageCount=pageCount+1
        }
   
        var dtb =testdiv.all[0];
        for(i=1;i<pageCount+1;i++){
            CPageFrame(win,iebg,i)
        }

        SplitPrintData_row = 1
        for(icc=1;icc<pageCount+1;icc++){
            itemHTML =css + "<body>" + hdtab +  SplitPrintData(icc,pageCount,pSize)  + "<script>window.parent.CreateSum(document)</script>";
            win.WriteHTML(icc-1,itemHTML);
        }
        if(win){
            setTimeout("win.resizeTo(screen.width ,screen.Height-30)",900);  //挪动位置,类似刷新功能
        }
        createselectfilds();    //创建选择的列
        testdiv.innerHTML="";
    }
    return obj;
}
function CPageFrame(win,iebg,index){
        var itemHTML ="";
        var imagediv=  null;
        var br = null;
        currIE= win.document.createElement("iframe");
        currIE.id="pages" + index;
        currIE.name = "pages" + index;
        currIE.frameborder="0";
        currIE.className="pageframe";
        iebg.appendChild(currIE);
        br=win.document.createElement("div")
        br.innerHTML="<br>"
        imagediv=win.document.createElement("div")
        imagediv.id="ImgPage" + index
        imagediv.innerHTML="&nbsp;"
        imagediv.style.width="100%";
        iebg.appendChild(br);
        iebg.appendChild(imagediv);
        currIE.src="about:blank";
        win.WriteHTML(index-1,"<html></html>");
}
function createselectfilds(){
    var i=0;
    var htm="<table style='font-size:12px'><tr><td>选择您要打印的列:</td>";
    var mtb= testdiv.all[0];
    if(mtb){
        if(mtb.rows.length>0){
            if(mtb.rows[0].cells.length>1){
                for(i=0;i<mtb.rows[0].cells.length;i++)
                {
                    mtit=mtb.rows[0].cells[i].innerText.Trim();
                    if(mtit.length==0){
                        mtit="第" + (i+1) + "列"
                    }
                    htm= htm + "<td style='padding:0 5 0 5 '><input type=checkbox checked id='fildscheckbox" + i +"' name='cbx' οnclick='if(!visiblefilds(" + i + ",this.checked)){this.checked=!this.checked}'><label for='fildscheckbox" + i + "' id='cbx"+ i +"'>" +  mtit  + "</label></td>"
                }
           
            }
        }
    }
    htm=htm + "</tr></table>"
    if(win){
        win.document.getElementById("Fildstd").innerHTML =htm;
    }
}
function SplitPrintData(index, pcount,pSize) {            //获取指定页的数据,对数据分页
    var datatab = testdiv.all[0];
    var psz = 0;
    if(!datatab){
        return "<table id='DataCell'  style='width:100%;text-align:left'><tr><td style='height:60;text-align:center'><br><br>没有要打印的数据</td></tr></table>"
    }
    var ntab  ="<table id='DataCell'  cellPadding=0 align=center cellspacing=0 style='width:100%;border-collapse:collapse'>"
    ntab = ntab + datatab.rows[0].outerHTML
    
    while(psz<pSize&&SplitPrintData_row<datatab.rows.length){
        ntab = ntab +  datatab.rows[SplitPrintData_row].outerHTML
        psz= psz +  datatab.rows[SplitPrintData_row].offsetHeight;
        SplitPrintData_row=SplitPrintData_row+1  //SplitPrintData_row用来记录本次调用函数所用掉的行数
    }
    ntab = ntab + "</table>"
    return ntab;
}
function ctestdiv(){
    testdiv = document.createElement("DIV");
    testdiv.style.position="absolute";
    testdiv.style.overflow="hidden";
    testdiv.style.height="0px";
    testdiv.style.width="190mm";
    testdiv.style.top="-1000";
    document.body.appendChild(testdiv);
}
function delspacechar(str){                 //删除空格
    return str.replace(//s/g,"")
}      
function getdocumenttitle(){                     //获取标题
      // var t = document.body.innerText.split("/n");
      //for(i=0;i<t.length;i++){
          //if(delspacechar(t[i]).length>0){
            // return t[i];
       // }
    //}
    return document.title;
}       
function createprintdata(imagefiled){ //提取要打印的数据; imagefiled 为图案字段索引号
 
    var itm = null;
    var htm ="";
    var hed ="";
    for(i=0;i<document.all.length;i++)
    {
        itm=document.all[i];
        if(itm.tagName=="TR"){
            if(itm.className.replace(" ","")=="GridViewHeaderStyle"){
                if(itm.style.display.toLowerCase()!="none"&&itm.style.visibility.toLowerCase()!="hidden"){
                    htm= doPostGvw(itm.parentElement,hed,imagefiled)
                    return htm;
                }
            }
        }
       
    }
}    
function doPostGvw(gvw,hd,imagefiled){ //imagefiled 为物料图案字段索引号

   var sender = ""
   var imgs = new Array();
   var tb=document.createElement("Table")
   var tr ,td ,i,ii,iobj,delone=false
   try{
        var fhead =delspacechar(gvw.rows[0].cells[0].innerText)
        if(fhead=="选择"||fhead=="移除"||fhead=="操作"){
            delone=true;
        }
   }
   catch(e){}
 
   if(imagefiled>-1){
         for(i=1;i<gvw.rows.length;i++)
         {
            sender = sender + gvw.rows[i].cells[imagefiled-1].innerText + "`"
         }
         var d = new Date;
         sender = window.GetTextByNet("ids=" +  sender + "&ptype=4&t=" + d.getTime());
         imgs = sender.split("`");
   }
  
   for(i=0;i<gvw.rows.length;i++){
        if(gvw.rows[i].className.length>1){
                tr = document.createElement("tr");
                if(imgs.length>0){
                    td=document.createElement("td");
                    if(i==0){
                        td.innerText= "图 案";
                    }
                    else{
                        td.innerHTML="<img style='width:80px;height:60px' src='" + imgs[i-1] + "' class='hb' />"
                    }
                    td.style.padding="4 4 4 4";
                    td.style.textAlign="center";
                    td.className="data";
                    tr.appendChild(td);
                } 
                for(ii=0;ii<gvw.rows[i].cells.length;ii++){
                    td=document.createElement("td");
                    if(delone!=true||ii>0){
                                if(gvw.rows[i].cells[ii].all.length==0){
                                    td.innerText=gvw.rows[i].cells[ii].innerText ;
                                }else{
                                    try{
                                        iobj=gvw.rows[i].cells[ii].all[0];
                                        if(iobj.tagName=="INPUT"){
                                            if(iobj.type.toLowerCase()=="text"){
                                                td.innerText=gvw.rows[i].cells[ii].all[0].value;
                                            }
                                        }
                                       
                                    }
                                    catch(e){
                                   
                                    }
                                }
                                td.className="data";
                                tr.appendChild(td);
                   }
                }
                tb.appendChild(tr);
         }
   }
   tb.style.borderCollapse="collapse";
   tb.align="center";
   tb.cellPadding="0";
   tb.cellSpacing="0";
   tb.style.width="100%";
  
   return tb.outerHTML;
}
var  root = new rootclass();
 
</textarea>
<textarea id="code2" cols="20" rows="2" runat='server'>

 
 <style type="text/css">
        button.funButton{
            border:1px outset;
            height:22px;
            width:80px;
            font-size:12px;
            font-family:宋体;
        }
        body{
            margin:0px;
            border:0px;
            overflow:auto;
        }
        .pageframe{
            text-align:left;
            width:200mm;
            height:280mm;
            background-color:white;
            overflow:hidden;
            padding:40px;
            border:1px solid #000000;
            border-right:3px solid #000000;
            border-bottom:3px solid #000000;
        }
        label{
            font-size:12px;
        }
 </style>
<table style='width:100%;height:100%;border-collapse:collapse'>
<tr>
        <td colspan=2 onselectstart='return false' style='height:66px;background-color:buttonface;border-bottom:1px outset'>
            <table style="width:100%;height:100%;border-collapse:collapse">
                <tr>
                    <td>
                        <button class='funButton' οnclick='cprint()'>打印</button>
                    </td>
                <%--    <td>
                        <button class='funButton' οnclick='CExcel()'>excel编辑</button>
                    </td>--%>
                   <%-- <td>
                        <button class='funButton' οnclick='CWord()'>word编辑</button>
                    </td>--%>
                    <td  style="width:100%">
                       
                    </td>
                    <td>
                        <button style="border:0px">每页文档高度:</button>
                    </td>
                    <td>
                        <select id='pSize' style="font-size:12px" οnchange='reloadpage(this.value)'>
                            <option value=500 >500px</option>
                            <option value=600 >600px</option>
                            <option value=700 selected>700px</option>
                            <option value=800>800px</option>
                            <option value=900 >900px</option>
                            <option value=1000 >1000px</option>
                            <option value=1100 >1100px</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td id='Fildstd' colspan='3' style="border-top:2px groove;">
                       
                    </td>
                    <td style="border-left:2px groove;border-top:2px groove;font-size:12px ; width :200px" colspan="1">
                    &nbsp;显示图片:<input type='checkbox' id='bagcheckbox' οnclick='if(this.checked){bagylcheckbox.checked=false};showImages(1)'><label for='bagcheckbox'>成品</label>
                    <input type=checkbox id='bagylcheckbox' οnclick='if(this.checked){bagcheckbox.checked=false};showImages(2)'><label for='bagylcheckbox'>物料</label>
                    </td>
                </tr>
            </table>
        </td>
</tr>
<tr>
        <td colspan=2 style='width:100%;text-align:center;background-color:#999999;cursor:default' valign=center>
            <div style='width:100%;height:100%;overflow:auto;padding:50px 20px 50px 20px' id='pagebg' oncontextmenu = 'return false'>
            </div>
            <OBJECT  id='WB'  classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2' style='display:none'></OBJECT>
        </td>
 </tr>
</table>   
 <script language='javascript'>
        self.focus();
        var spc= ""
        var rgset = 0;
        var Wsh = 0;
        var ylImageIndex = -1 ;
        var HKEY_Root,HKEY_Path,HKEY_Key;
        HKEY_Root="HKEY_CURRENT_USER";  
        HKEY_Path="//Software//Microsoft//Internet Explorer//PageSetup//";
        for(i=0;i<400;i++){
            spc=spc+ "&nbsp;"
        }
        function SetPage(pIndex,pCount){
             HKEY_Key="header";
             if(Wsh){  
                Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b第" + pIndex + "页 / 共" + pCount + "页");  
             }
        }
        //设置网页打印的页眉页脚为空  
        function PageSetup_Null()  
        {  
            if(rgset!=0){return}
           
            rgset=1;
            try 
            {  
                Wsh=new ActiveXObject("WScript.Shell");  
                HKEY_Key="header";  
                Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");  
                HKEY_Key="footer";  
                Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");  
            }  
            catch(e)  
            {}
        }
           
        function cprint(){
            PageSetup_Null(); 
            var ii=0, fLen=0;
            var aFrms = parent.document.getElementsByTagName("IFRAME")
            for(i=0;i<aFrms.length;i++){
                if(aFrms[i].style.display.toLowerCase()!="none"){   //contentWindow==childframe){
                    fLen =fLen +1;
                }
            }
            for(i=0;i<aFrms.length;i++){
                if(aFrms[i].style.display.toLowerCase()!="none"){   //contentWindow==childframe){
                    obj=aFrms[i].contentWindow;
                    ii=ii+1
                    SetPage(ii,fLen);;
                    obj.focus();
                    WB.ExecWB(6,6);
                    break;
                }
            }
        }
        function WriteHTML(fIndex,html){
            var domc = window.frames[fIndex].document
            domc.write(html + "/n/0");
        }  
        function CreateSum(dom){ //统计表格数据
            var i,ii,k,acc=-1,tr,td,hs;
            var aCell = new Array()
            var aindex = new Array()
            tab = dom.all["DataCell"];
            for(i=0;i<tab.rows[0].cells.length;i++){
                k=tab.rows[0].cells[i].innerText.replace(//s/g,"")
               
                if(k=="数量"||k=="金额"){
                        acc=acc+1
                        aCell[acc]=0;
                        aindex[acc]=i;
                }
            }
            for(i=1;i<tab.rows.length;i++){
                for(ii=0;ii<acc+1;ii++){
                    aCell[ii]= aCell[ii] + tab.rows[i].cells[aindex[ii]].innerText*1
                }
            }
            tr=tab.insertRow(-1);
            td=tr.insertCell(-1);
            td.className="tjcell";
            td.innerText="合计"
            for(i=1;i<tab.rows[0].cells.length;i++){
                td=tr.insertCell(-1);
                td.innerText=""
                for(ii=0;ii<acc+1;ii++){
                     if(aindex[ii]==i){
                        td.className="tjcell";
                        td.innerText=aCell[ii].toFixed(3);
                       
                     }
                }

            }
          
        }
      
        function visiblefilds(si,v){
             var i,ii,v
             var sv="";
             var sibc=0;
             if(v){
                sv="block"
             }
             else{
                sv="none"
             }
             for(i=0;i<window.frames.length;i++){
                obj=window.frames[i].document.getElementById("DataCell")
                if(obj){
                    if(obj.rows.length>0){
                        sibc=0;
                        for(ii=0;ii<obj.rows[0].cells.length;ii++){
                            if(obj.rows[0].cells[ii].style.display!="none"){
                                sibc=sibc+1
                            }
                        }
                        if(sibc>2||v==true){
                            for(ii=0;ii<obj.rows.length;ii++){
                                obj.rows[ii].cells[si].style.display=sv;
                            }
                        }
                        else
                        {
                            alert("对不起,至少需显示两列数据");
                            return false;
                        }
                    }
                }
               
            }
          
            movewindow();
            return true;
        }
         var visi_move_value = 0;
        function movewindow(){
            if(visi_move_value==0){
                visi_move_value=1;
            }
            else{
                visi_move_value=0;
            }
            setTimeout("window.resizeTo(screen.width - "+ visi_move_value +" ,screen.Height-30)",900);
        }
        function GetAllHTML(){
             var htm ="";
             for(i=0;i<document.frames.length;i++){
                obj=document.frames[i];
                if(i==0){
                    htm= htm + "<br>" + obj.document.body.innerHTML;
                }
                else{
                    htm = htm + "/n" + obj.document.all["DataCell"].outerHTML;
                }
            }
            return htm;
        }
     function CExcel(){
                var htm =  GetAllHTML();
                try{
                     var oXL = new ActiveXObject("Excel.Application");
                     var oWB = oXL.Workbooks.Add();
                     var oSheet = oWB.ActiveSheet;
                }catch(e){
                 alert("无法调用Excel对象。 /n请确认已经安装了office软件,并且浏览器安全级别设置允许本系统访问客服端组件");
                 return false;
                }
                window.clipboardData.setData("Text",htm);
                oSheet.Paste();
                oSheet.Cells(1, 1).Select();
                oXL.Visible = true;
                window.clipboardData.setData("Text","");
        }
        function  CWord(){
                var i,ii,iii,mtb
                try{
                   var oWD = null // new ActiveXObject("Word.Application");
                  }catch(e)
                  {
                   alert("无法调用Word对象。/n请确认已经安装了office软件,并且浏览器安全级别设置允许本系统访问客服端组件");
                   return;
                  }
                var oDC = oWD.Documents.Add("");
                var  dvs = document.createElement("Div")
                dvs.innerHTML= GetAllHTML();
                range=oDC.range();


                 window.clipboardData.setData("Text","/n" + dvs.innerText);

                range.Paste();

                oWD.Application.Visible = true;
           
        }
        function showImages(ylcls){
             var i=0,IDFieldsIndex=0,ii=0
             var bgobj =document.getElementById("Fildstd");
             for(i=0;i<bgobj.all.length;i++){
                var elem =bgobj.all[i];
                if(elem.tagName.toLowerCase()=="label"){
                    ii=ii+1
                    ylclass=getylclass(elem.innerText.replace(//s/g,""))
                    if(ylclass>0){
                        IDFieldsIndex=ii
                        break;
                    }
                }
             }
           
             if(ylcls>-1){
                ylclass=ylcls;
             }
             else{
                return;
             }
             if(ylcls==2){
                if(bagylcheckbox.checked){
                    ylImageIndex= IDFieldsIndex ;
                    window.setTimeout("reloadpage(document.getElementById('pSize').value)",1000);
                }
                else{
                    ylImageIndex = -1;
                    window.setTimeout("reloadpage(document.getElementById('pSize').value)",1000);
                }
                return false;
             }
             var  fl= document.frames.length
             for(i=0;i<fl;i++){

                createImagesPage(i,ylclass,IDFieldsIndex)
            }    
        }
        function getylclass(v){ //根据表头文字获取该表物料或成品类型,1为成品,2为物料
                if(v.indexOf("编号")>-1){
                    return 1;
                }
                if(v.indexOf("款号")>-1){
                    return 1;
                }
                if(v.indexOf("公司编码")>-1){
                    return 2;
                }
                if(v.indexOf("物料编码")>-1){
                    return 2;
                }
                if(v.indexOf("物料编号")>-1){
                    return 2;
                }
                return 0;
        }
        function createImagesPage(PageIndex,ylClass,fieldsIndex){
             var i,ids="",newhtm="";
             var d = new Date();
             var pindex = PageIndex + 1
             var ss = ("ImgPage" + pindex)
             var imgdiv = document.getElementById(ss)
             if(!imgdiv){
      
                return ;
             }
             imgdiv.innerHTML="";
             if(!bagcheckbox.checked&&!bagylcheckbox.checked){
                return;
             }    
            
             var pg=window.frames["pages" + pindex]
             if(pg){
                pg=pg.document.all["DataCell"]
             }
             else{
                return;
             }
             if(pg){
                //alert(PageIndex)
                for(i=1;i<pg.rows.length-1;i++){
                     ids=ids + pg.rows[i].cells[fieldsIndex-1].innerText + "`";
                }
             }
             else{
                //alert("--" + PageIndex)
             }
            
             htm=window.opener.GetTextByNet("ptype=3&ids=" + ids + "&ylcls=" + ylClass + "&t=" + d.getTime());
            
             htm = htm.split("`")
             for(i=0;i<htm.length-1;i++){
                newhtm=newhtm + "<iframe class='pageframe' frameborder='no'  src='about:blank' name='"  +  imgdiv.id +  "_ie_" + i + "'></iframe><br><br>";
             }
           
             imgdiv.innerHTML=newhtm;
             for(i=0;i<htm.length-1;i++){
                mname= imgdiv.id +  "_ie_" + i
                for(ii=0;ii<document.frames.length;ii++){
                    if(document.frames[ii].name==mname){
                        document.frames[ii].document.write( "<style>span{height:22;font-size:12px;padding-top:2px}img.hb{}img{width:32mm;height:40mm;border:1px solid #eeeeee}body{margin:0px;overflow:hidden}td{width:33mm;text-align:center}</style><center style='font-weight:bold'>" + window.opener.root.title().replace("管理","").replace("查询","").replace("开","") + " - 图片</center><br>" + htm[i]);
                    }
                }
               
             }
             BindNuem(); //创建右键菜单
        }
   
        document.body.onload = function (){
            window.setTimeout("reloadpage(700)",1000);
        }
        function reloadpage(Psize){

            var root = window.opener.root;
            document.all['pagebg'].innerHTML="";
            document.title = "打印预览 - [" +  root.title() + "]" + spc;
           
            root.loaddata(document.all['pagebg'],Psize, ylImageIndex);
           
            BindNuem(); //创建右键菜单
        }
       
        function BindNuem(){//创建右键菜单
            var icc = 0 ;
            for(icc=0;icc<window.frames.length;icc++){
                window.frames[icc].execScript("document.oncontextmenu = function(){return window.parent.RightNuem(window);}","javascript")
            } 
        }
       
        //=====================下面是弹出菜单=========================
        var childframe = null;
        var childevent = null;
        var oPopup =  null;
        function RightNuem(winobj){
            var delable= "";
            oPopup = winobj.createPopup();
            var oPopBody = oPopup.document.body;
            oPopBody.style.backgroundColor = "buttonface";
            oPopBody.style.border = "2px outset";
            oPopBody.style.margin="2 2 2 2";
            //if(winobj.event.srcElement.tagName.toLowerCase()!="td"){
               delable= "disabled='true'"
            //}
            var xhtm  =  "<table style='padding-left:5px;font-size:12px;width:100%;height:140px;cursor:default;border-collapse:collapse' oncontextmenu ='return false' onselectstart='return false' >" +
                         "<tr><td style='height:18px;padding-top:2' οnclick='parent.parent.ppClick(1);parent.parent.pput(this)' οnmοusemοve='parent.parent.ppmv(this)' οnmοuseοut='parent.parent.pput(this)'>&nbsp;全 选</td></tr>" +
                         "<tr><td style='height:18px;padding-top:2' οnclick='parent.parent.ppClick(2);parent.parent.pput(this)' οnmοusemοve='parent.parent.ppmv(this)' οnmοuseοut='parent.parent.pput(this)'>&nbsp;复 制</td></tr>" +
                         "<tr><td style='height:18px;padding-top:2' οnclick='parent.parent.ppClick(3);parent.parent.pput(this)' οnmοusemοve='parent.parent.ppmv(this)' οnmοuseοut='parent.parent.pput(this)'>&nbsp;打印该页</td></tr>" +
                         "<tr><td style='height:1px'></td></tr>" +
                         "<tr><td style='border-top:2px groove;height:2px'></td></tr>" +
                         "<tr><td style='height:18px;padding-top:2' οnclick='parent.parent.ppClick(4);parent.parent.pput(this)' οnmοusemοve='parent.parent.ppmv(this)' οnmοuseοut='parent.parent.pput(this)'>&nbsp;删除该页</td></tr>" +
                         //"<tr><td style='height:18px;padding-top:2;display:none' οnclick='parent.parent.ppClick(5);parent.parent.pput(this)' οnmοusemοve='parent.parent.ppmv(this)' οnmοuseοut='parent.parent.pput(this)' " + delable + " >&nbsp;删除该行</td></tr>" +
                         "<tr><td style='height:18px;padding-top:2' οnclick='parent.parent.ppClick(6);parent.parent.pput(this)' οnmοusemοve='parent.parent.ppmv(this)' οnmοuseοut='parent.parent.pput(this)'>&nbsp;编辑页面</td></tr>" + 
                         "<//table>"            
            oPopBody.innerHTML = xhtm
   
            oPopup.show(winobj.event.x-2, winobj.event.y-2, 180, 148, winobj.document.body);

            childevent = winobj.event.srcElement;
            childframe = winobj;
            return false ;
        }
        function ppmv(td){
                td.style.backgroundColor='#444477';
                td.style.color='white';
                popoShow()
        }
        function pput(td){
                td.style.backgroundColor='buttonface';
                td.style.color='#000000';
                popoShow()
        }
        function popoShow(){
            if(oPopup){
                //oPopup.document.focus();
            }
        }
        function setedit(){
            var htm = "";
            if(childframe.document.designMode.toLowerCase()!=="on"){
                htm = childframe.document.all[0].innerHTML;
                childframe.focus();
                childframe.document.all["DataCell"].contentEditable='true';
                childframe.document.designMode="On";
                childframe.document.focus();
                //childframe.src="about:blank";
                //childframe.document.write(htm);
            }
            else{
                childframe.document.designMode="Off"
            }
        }
        function ppClick(cmdType){
            var i,ii
            switch(cmdType){
                case 1:
                    childframe.document.execCommand("SelectAll");
                    break;
                case 2:
                    childframe.document.execCommand("Copy");
                    break;
                case 3:
                    childframe.focus();
                    WB.ExecWB(6,6);
                    break;
                case 4:
                    if(window.confirm("您确定删除该页吗?")){
                        var aFrms = parent.document.getElementsByTagName("IFRAME")
                        for(i=0;i<aFrms.length;i++){
                            if(aFrms[i].contentWindow==childframe){
                       
                                aFrms[i].style.display="none";
                                break;
                            }
                        }
                    }
                    break;
                case 6:
                    if(window.confirm("您确定要手动编辑该报表吗?")){
                        window.setTimeout("setedit()",1000)
                    }
                    break;
                 
            }
            try{
                oPopup.hide();
            }
            catch(e){
           
            }
        }
       
       
 </script>

       
        </textarea>
   
    </div>
    </form>
</body>
</html>

这篇关于JS获取页面数据并打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/817321

相关文章

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)