ITeye博客

js调用excel、word打印

microjava · 2010-07-13 20:46 · 综合技术 · Web前端 · Markdown

js调用excel、word打印


<script type="text/javascript">

printExcel('d:/800031.xls');

function  printExcel(obj)
{
   var xlsApp = null;    
    try{        
        xlsApp = new ActiveXObject('Excel.Application');    }catch(e) 
    { 
        alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Excel对象或者客户端没有安装Excel软件'); 
          return; 
    }    
    //var xlBook = xlsApp.Workbooks.Open('http://'+window.location.host+obj.value);
    var xlBook = xlsApp.Workbooks.Open(obj);
    var xlsheet = xlBook.Worksheets(1);
    xlsApp.Application.Visible = false; 
    xlsApp.visible = false; 
    xlsheet.Printout; 
    xlsApp.Quit(); 
    xlsApp=null;    
}

</script>

excel




function  openExcel(obj)
{
  // var xlsApp = null;
    //try{
    //    xlsApp = new ActiveXObject('Excel.Application');    }catch(e)
    //{
    //    alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Excel对象或者客户端没有安装Excel软件');
    //      return;
   // }
   //var s='http://'+window.location.host+obj.replace(/(file:\/\/)/g,'/report/');
   //var xlBook = xlsApp.Workbooks.Open(s);
   //xlsApp.Application.Visible = true;
   //xlsApp.visible = true;
   var s=obj.replace(/(file:\/\/)/g,'/report/');
   window.showModalDialog('/ocx/attachshow.jsp?xlsName='+s,'报表预览','dialogWidth=1024px;dialogHeight=800px;status=no;help=no;scroll=no;location=no');
}

function  printExcels(obj)
{
     var xlsApp = null;    
     try{        
     xlsApp = new ActiveXObject('Excel.Application');    }catch(e) 
     { 
     alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Excel对象或者客户端没有安装Excel软件'); 
       return; 
     }
     var s='http://'+window.location.host+obj.replace(/(file:\/\/)/g,'/report/');
     var ss = s.split("/");
     if(!(setFlag(ss[ss.length-2])))
          return;
     var xlBook = xlsApp.Workbooks.Open(s);
     
          //var xlsheet;
     
          try{
         
             //for(printSheetLen=1;printSheetLen<=xlBook.Sheets.Count;printSheetLen++){
           
              //      xlsheet = xlBook.Worksheets(printSheetLen); 
     
         //    xlsApp.Application.Visible = false; 
     
               //  xlsApp.visible = false; 
              
               // xlsheet.Printout; 
       
               //}
     
               xlBook.Printout;
          }
     
          catch(e){
          
               alert(e);
     
          }
     finally{
          xlsApp.Quit(); 
          xlsApp=null; 
     //     clearPrintFlag();
     }
}

word



function  printWord(obj)
{
   var wordApp = null;    
    try{        
        wordApp = new ActiveXObject('Word.Application');    }catch(e) 
    { 
        alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件'); 
          return; 
    }    
    var   Doc=wordApp.Documents.Open('http://'+window.location.host+obj.value);   
    wordApp.Application.Visible = false; 
    wordApp.visible = false; 
    wordApp.ActiveDocument.printout(); 
    wordApp.ActiveDocument.close(); 
    wordApp.Quit(); 
    wordApp=null;    
}

测试查询


<script type="text/javascript">

printWord('d:/test.doc');


function  printWord(obj)
{
   var wordApp = null;    
    try{        
        wordApp = new ActiveXObject('Word.Application');    }catch(e) 
    { 
        alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件'); 
          return; 
    }    
    //var   Doc=wordApp.Documents.Open('http://'+window.location.host+obj.value);
    var   Doc=wordApp.Documents.Open(obj); 
    wordApp.Application.Visible = false; 
    wordApp.visible = false; 
    wordApp.ActiveDocument.printout(); 
    wordApp.ActiveDocument.close(); 
    wordApp.Quit(); 
    wordApp=null;    
}

</script>

评论(1)

  1. #1 楼 · July01 · 2017-09-27 13:08

    最近了解到一款StratoIO打印控件, 功能如下:1、Html友好的打印方式 2、兼容主流浏览器 3、灵活配置的任务 4、交互式打印支持 5、丰富的管理面板和接口 6、个性化服务 浏览器和系统的兼容性都很好,而且不会崩溃。参考http://webprint.stratoio.com/features

评论功能维护中,当前仅展示历史评论。