JavaScript实现打印页面表报,表单输出到word和excel功能

来源:互联网 发布:程序员转行干什么 编辑:程序博客网 时间:2024/04/29 17:30
http://blog.csdn.net/dhdhdh0920/article/details/4032140
<html><head><meta name=vs_targetSchema   content="http://schemas.microsoft.com/intellisense/ie5">  <mce:style media=print><!--    .Noprint{display:none;}    .PageNext{page-break-after:always;}  --></mce:style><style media=print mce_bogus="1">    .Noprint{display:none;}    .PageNext{page-break-after:always;}  </style></head><body >  <center class="Noprint">   <p>     <OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2  height=0 width=0></OBJECT>        <input type=button value=打印  onclick=document.all.WebBrowser.ExecWB(6,1)>      <input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1)>        <input type=button value=打印预览 onclick=document.all.WebBrowser.ExecWB(7,1)>      <input type=button value=输出到word onclick=outDoc() >  <input type=button value=输出到excel onclick=outExcel() >    </p>  </center><form action=# method="post" name=form1>  <table id="Table">    <tr><td>1</td><td>2</td><td>3</td></tr>        <tr><td>4</td><td>5</td><td>6</td></tr>    <tr><td>7</td><td>8</td><td>9</td></tr>    <tr><td>10</td><td>11</td><td>12</td></tr>  </table></form></body></html><mce:script type="text/javascript"><!-- function outDoc(){ var table=document.getElementById("Table"); row=table.rows.length; column=table.rows(1).cells.length; var wdapp=new ActiveXObject("Word.Application"); wdapp.visible=true; wddoc=wdapp.Documents.Add();  //添加新的文档 thearray=new Array();//将页面中表格的内容存放在数组中for(i=0;i<row;i++){   thearray[i]=new Array();   for(j=0;j<column;j++){       thearray[i][j]=table.rows(i).cells(j).innerHTML;   }}var range = wddoc.Range(0,0);range.Text=getValue+"/n";wdapp.Application.Activedocument.Paragraphs.Add(range);wdapp.Application.Activedocument.Paragraphs.Add();rngcurrent=wdapp.Application.Activedocument.Paragraphs(3).Range;var objTable=wddoc.Tables.Add(rngcurrent,row,column)     //插入表格for(i=0;i<row;i++){   for(j=0;j<column;j++){   objTable.Cell(i+1,j+1).Range.Text = thearray[i][j].replace(" ","");   }}wdapp.Application.ActiveDocument.SaveAs("test.doc",0,false,"",true,"",false,false,false,false,false);     //保存到Word的默认文档保存路径中//wdapp.Application.Printout();     //自动打印wdapp=null;}//输出到excel   function outExcel(){   var table=document.getElementById("Table");   row=table.rows.length;   column=table.rows(1).cells.length;   var excelapp=new ActiveXObject("Excel.Application");   excelapp.visible=true;   objBook=excelapp.Workbooks.Add(); //添加新的工作簿   var objSheet = objBook.ActiveSheet;   for(i=0;i<row;i++){      for(j=0;j<column;j++){     objSheet.Cells(i+1,j+1).value=table.rows(i).cells(j).innerHTML.replace(" ","");      }   }        objBook.SaveAs("test.xls");   //objSheet.Printout;     //自动打印   excelapp.UserControl = true;}// --></mce:script>
原创粉丝点击