本文主要是介绍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 + " 地址:" + myAddress + " "
}
if(myPhone.length >0) {
addrbar = addrbar + " 电话:" + myPhone + " "
}
if(myFax.length >0) {
addrbar = addrbar + " 传真:" + myFax + " "
}
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"," " + elements[i].value+ " ");
}
else{
obj.setAttribute("innerHTML"," 无 ");
}
break;
case "textarea" :
obj.setAttribute("innerHTML"," " + elements[i].innerHTML + " ");
elements[i].innerHTML = '';
break;
case "select-one" :
for(var j=0;j<elements[i].length;j++)
{
if(elements[i][j].selected)
{
obj.setAttribute("innerHTML"," " + elements[i][j].text + " ");
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=" "
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">
显示图片:<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+ " "
}
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)'> 全 选</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)'> 复 制</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)'> 打印该页</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)'> 删除该页</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 + " > 删除该行</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)'> 编辑页面</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获取页面数据并打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!