<pre>对js的打印方法总结一下,方便日后查阅。一.用js自带函数打印直接调用java代码 复制代码 1. <a href="javascript:window.print();">打印</a> <a href="javascript:window.print();">打印</a>二.iewebbrowser组件介绍
http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3bq267240#top
http://support.microsoft.com/kb/q247671/#appliestojava代码 复制代码 1. <object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=0 id=webbrowser width=0></object> 2. <input name=button onclick=document.all.webbrowser.execwb(1,1) type=button value=打开> 3. <input name=button onclick=document.all.webbrowser.execwb(2,1) type=button value=关闭所有> 4. <input name=button onclick=document.all.webbrowser.execwb(4,1) type=button value=另存为> 5. <input name=button onclick=document.all.webbrowser.execwb(6,1) type=button value=打印> 6. <input name=button onclick=document.all.webbrowser.execwb(6,6) type=button value=直接打印> 7. <input name=button onclick=document.all.webbrowser.execwb(7,1) type=button value=打印预览> 8. <input name=button onclick=document.all.webbrowser.execwb(8,1) type=button value=页面设置> 9. <input name=button onclick=document.all.webbrowser.execwb(10,1) type=button value=属性> 10. <input name=button onclick=document.all.webbrowser.execwb(17,1) type=button value=全选> 11. <input name=button onclick=document.all.webbrowser.execwb(22,1) type=button value=刷新> 12. <input name=button onclick=document.all.webbrowser.execwb(45,1) type=button value=关闭> <object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=0 id=webbrowser width=0></object> <input name=button onclick=document.all.webbrowser.execwb(1,1) type=button value=打开><input name=button onclick=document.all.webbrowser.execwb(2,1) type=button value=关闭所有><input name=button onclick=document.all.webbrowser.execwb(4,1) type=button value=另存为> <input name=button onclick=document.all.webbrowser.execwb(6,1) type=button value=打印><input name=button onclick=document.all.webbrowser.execwb(6,6) type=button value=直接打印><input name=button onclick=document.all.webbrowser.execwb(7,1) type=button value=打印预览><input name=button onclick=document.all.webbrowser.execwb(8,1) type=button value=页面设置><input name=button onclick=document.all.webbrowser.execwb(10,1) type=button value=属性><input name=button onclick=document.all.webbrowser.execwb(17,1) type=button value=全选><input name=button onclick=document.all.webbrowser.execwb(22,1) type=button value=刷新><input name=button onclick=document.all.webbrowser.execwb(45,1) type=button value=关闭>三.使用scriptx.cab控件1.下载scriptx.cab控件官网
http://www.meadroid.com/scriptx/index.asp2.使用object元素,修改codebase,classid的值这里调用控件scriptx.cabjava代码 复制代码 1. <object id="factory" style="display: none" codebase="${rooturl}js/smsx.cab#vversion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></object> <object id="factory" style="display: none" codebase="${rooturl}js/smsx.cab#vversion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></object>这段代码用来加载cab文件,clsid和codebase必须要和你下载的cab中的信息对应,否则组件会加载错误,这两项其实不难找,只要你用winrar打开你下载的cab文件,然后找到扩展名是.inf的文件,然后打开之,就能看到了。3.调用控件脚本print.js文件java代码 复制代码 1. function setprintbase(headertext,footertext,rooturl) { 2. 3. // -- advanced features ,未曾使用过,有待确认。 4. 5. //factory.printing.setmarginmeasure(2); // measure margins in inches 6. 7. //factory.setpagerange(false, 1, 3);// need pages from 1 to 3 8. 9. //factory.printing.printer = "hp deskjet 870c"; 10. 11. //factory.printing.copies = 2; 12. 13. //factory.printing.collate = true; 14. 15. //factory.printing.papersize = "a4"; 16. 17. //factory.printing.papersource = "manual feed" 18. 19. var header = (headertext==null||headertext=="")?'默认页眉':headertext; 20. 21. var footer = (footertext==null||footertext=="")?'默认页角':footertext; 22. 23. factory.printing.header = "&b"+header+"&b" ; 24. 25. factory.printing.footer = "&b"+footer; 26. 27. factory.printing.portrait = true; 28. 29. factory.printing.leftmargin =10.00; 30. 31. factory.printing.topmargin =10.00; 32. 33. factory.printing.rightmargin =10.00; 34. 35. factory.printing.bottommargin =10.00; 36. 37. } function setprintbase(headertext,footertext,rooturl) { // -- advanced features ,未曾使用过,有待确认。 //factory.printing.setmarginmeasure(2); // measure margins in inches //factory.setpagerange(false, 1, 3);// need pages from 1 to 3 //factory.printing.printer = "hp deskjet 870c"; //factory.printing.copies = 2; //factory.printing.collate = true; //factory.printing.papersize = "a4"; //factory.printing.papersource = "manual feed" var header = (headertext==null||headertext=="")?'默认页眉':headertext; var footer = (footertext==null||footertext=="")?'默认页角':footertext;factory.printing.header = "&b"+header+"&b" ;factory.printing.footer = "&b"+footer;factory.printing.portrait = true;factory.printing.leftmargin =10.00;factory.printing.topmargin =10.00;factory.printing.rightmargin =10.00;factory.printing.bottommargin =10.00;}例子java代码 复制代码 1. <%@ page contenttype="text/html;charset=gbk"%> 2. 3. <html> 4. <head> 5. <meta http-equiv="imagetoolbar" content="no"> 6. <script language="javascript" src="print.js"></script> 7. <style media="print"> 8. .noprint {display: none;} 9. </style> 10. <title>打印测试</title> 11. </head> 12. <object id="factory" style="display: none" codebase="smsx.cab#vversion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></object> 13. 14. <script defer> 15. function window.onload() { 16. setprintbase('页眉','页脚'); 17. } 18. </script> 19. <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"> 20. <center class="noprint"> 21. <input type=button value="打印" onclick="factory.printing.print(true)"> 22. <input type=button value="页面设置" onclick="factory.printing.pagesetup()"> 23. <input type=button value="打印预览" onclick="factory.printing.preview()"> 24. <input type="button" value="关闭" onclick="window.close();"> 25. </center> 26. <center> 27. <table width="100%" border="0" cellpadding="0" cellspacing="0"> 28. <tr><td align="center"><b>内容</b></td></tr> 29. </table> 30. </center> 31. </body> 32. </html> <%@ page contenttype="text/html;charset=gbk"%><html><head><meta http-equiv="imagetoolbar" content="no"><script language="javascript" src="print.js"></script><style media="print">.noprint {display: none;}</style><title>打印测试</title></head><object id="factory" style="display: none" codebase="smsx.cab#vversion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></object><script defer>function window.onload() { setprintbase('页眉','页脚');}</script><body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0"><center class="noprint"><input type=button value="打印" onclick="factory.printing.print(true)"> <input type=button value="页面设置" onclick="factory.printing.pagesetup()"> <input type=button value="打印预览" onclick="factory.printing.preview()"> <input type="button" value="关闭" onclick="window.close();"></center> <center> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr><td align="center"><b>内容</b></td></tr> </table> </center></body></html>四.对比1.window.print调用方便,但功能简单2.功能更强大,但使用iewebbrowser有时会报js没有权限的错误。3.scriptx控件功能也比较强大,目前在使用这种方式。这里的也不错啊,有打印到 word的和excel
http://hi.baidu.com/afei0211/blog/item/20523955ca8bebc2b745ae98.html
http://hi.baidu.com/hongz1125/blog/item/0152bcfd84ce1e1008244d5c.html.net环境下的
http://blog.csdn.net/flygoldfish/archive/2004/08/17/77208.aspx </pre>