JS+调用word打印功能实现在Webfrom客户端

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml" >
<! DOCTYPE html >  
< html >  
< head  runat ="server" >  
     < title >调用Word打印机打印 </ title >  
     < link  rel ="stylesheet"  href ="style.css"  type ="text/css"   />  
</ head >  
< body >  
     < div  class ="noprint" >  
         < input  type ="button"  value ="toPrinter "  onclick ="doPrint();"   />  
     </ div >  
     < script  type ="text/javascript" >
        
function  doPrint() {
            viewToWord(
" 纳思达:04\r\n栋号:3\r\n楼层:1\r\n户型\r\n时间:2013年1月10日 " );
        }
        
var  wdapp;
        
var  wddoc;
        
function  viewToWord(str) {
            
try  {
                
// 获取Word 过程  
                 // 请设置IE的可信任站点  
                wdapp  =   new  ActiveXObject( " Word.Application " );
            }
            
catch  (e) {
                alert(
" 无法调用Office对象,请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中! " );
                wdapp 
=   null ;
                
return ;
                        }

            wdapp.Documents.Open(
" c:\\1.doc " );  // 打开本地(客户端)word模板  
            wddoc  =  wdapp.ActiveDocument;
           
//  wddoc.Bookmarks("Title").Range.Text = "要打印的标题"; //找到Word中的Title标签,替换其内容  
             // wddoc.Bookmarks("Content").Range.Text = str;
            wdapp.visible  =   false // word模板是否可见  
            wddoc.saveAs( " c:\\PrinterTemp.doc " );  // 保存临时文件word  
            wdapp.Application.Printout();  // 调用自动打印功能  

            wdapp.quit();
            wdapp 
=   null ;
            wddoc.quit();
            wddoc 
=   null ;
        }  
    
</ script >  
</ body >  
</ html >  

 

 

 

接下来设置Word模版:
JS+调用word打印功能实现在Webfrom客户端


选中“标题:”,选择插入标签,输入标签的名称;

JS+调用word打印功能实现在Webfrom客户端
打印机打印的就是这个word上的内容了。

运行打印后会生成一个这样的临时文件,看到了吗?标签的内容被替换了。
内容:部分同理。
最后还要设置浏览器的安全
JS+调用word打印功能实现在Webfrom客户端


呵呵,这样就可以在web客户端下打印小票了!
此方法由http://blog.sina.com.cn/s/blog_692d0a650100klr4.html启发!

 

http://blog.csdn.net/weizengxun/article/details/6870880

你可能感兴趣的:(word)