New Document
dfsdfds | &dsfds | &sdf; | dsf |
dsf | dsfds | fsdfdsfdsf | dsfs |
dsfds | dsf | dsfd | sfds |
sdf | fdsf |
nextpage
dfsdfds | &dsfds | &sdf; | dsf |
dsf | dsfds | fsdfdsfdsf | dsfs |
dsfds | dsf | dsfd | sfds |
sdf | fdsf |
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <title> New Document </title> 5 <meta name="Generator" content="EditPlus"> 6 <meta name="Author" content=""> 7 <meta name="Keywords" content=""> 8 <meta name="Description" content=""> 9 </head> 10 <script language="javascript"> 11 function outputWord(elementID,HorV,pagekey){ 12 var sel = document.body.createTextRange(); //var sel = object.createTextRange()创建可操作区域,用来在内从中编辑,sel.select()为全选,sel.execCommand("Copy");执行复制命令 13 var obj = document.getElementById(elementID);//获取网页对象 14 sel.moveToElementText(obj); //将sel区域选中到obj对象结尾 15 sel.select(); 16 sel.execCommand("Copy"); //执行复制命令,将内容放入粘贴板 17 try{ 18 var wordObj = new ActiveXObject("Word.Application"); //创建Word应用程序对象wordObj 19 }catch(e) 20 { 21 alert("无法调用Office对象,请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中!"); 22 return; 23 } 24 var word = wordObj.Documents.Add("",0,1);//不打开模版直接加入内容,创建空白Word文档 25 //var word = wordObj.documents.open("c:\\ba.doc"); //此处为打开已有的模版,打开已存word文档,会把新内容插入文档最前端 26 if (HorV!="") 27 { 28 //横向打印设置部分 29 word.PageSetup.LineNumbering.Active = false; 30 word.PageSetup.Orientation. wdOrientLandscape = 1; //设置为横向打印 31 word.PageSetup.TopMargin = word.Application.CentimetersToPoints(3.17); 32 word.PageSetup.BottomMargin = word.Application.CentimetersToPoints(3.17); 33 word.PageSetup.LeftMargin = word.Application.CentimetersToPoints(2.54); 34 word.PageSetup.RightMargin = word.Application.CentimetersToPoints(2.54); 35 word.PageSetup.Gutter = word.Application.CentimetersToPoints(0); 36 word.PageSetup.HeaderDistance = word.Application.CentimetersToPoints(1.5); 37 word.PageSetup.FooterDistance = word.Application.CentimetersToPoints(1.75); 38 word.PageSetup.PageWidth = word.Application.CentimetersToPoints(29.7); 39 word.PageSetup.PageHeight = word.Application.CentimetersToPoints(21); 40 word.PageSetup.FirstPageTray = 0; 41 word.PageSetup.OtherPagesTray = 0; 42 word.PageSetup.SectionStart = 2; 43 word.PageSetup.OddAndEvenPagesHeaderFooter = false; 44 word.PageSetup.DifferentFirstPageHeaderFooter = false; 45 word.PageSetup.VerticalAlignment = 0; 46 word.PageSetup.SuppressEndnotes = false; 47 word.PageSetup.MirrorMargins = false; 48 word.PageSetup.TwoPagesOnOne = false; 49 word.PageSetup.BookFoldPrinting = false; 50 word.PageSetup.BookFoldRevPrinting = false; 51 word.PageSetup.BookFoldPrintingSheets = 1 52 word.PageSetup.GutterPos = 0; 53 word.PageSetup.LayoutMode = 2; 54 } 55 word.ShowGrammaticalErrors = false; //屏蔽语法检查 56 word.ShowSpellingErrors = false; //屏蔽拼写错误 57 //var oRange = word.Range(0,1);//在word中创建连续区域,打开文档的区域,选中0到1的字符 58 //oRange.Paste(); //粘贴1:将粘贴板内容存入word选中区域中 59 wordObj.Selection.Paste();//粘贴2:或者直接粘贴到新建的word中 60 61 if (pagekey!="") 62 { 63 var selection = wordObj.Selection;//得到打开后word的selection对象 64 //selection.Font.Size = 10; //设置字体大小 65 selection.WholeStory(); //ctrl+A 全选操作 66 selection.Find.ClearFormatting(); //清除格式 67 selection.Find.Text = pagekey; //指定查找关键字 68 selection.Find.Forward = true; //向下查找 69 selection.Find.Wrap = 1;//向下查找 70 selection.Find.MatchCase = false; //不区分大小写 71 selection.Find.MatchWholeWord = false; //不匹配整个单词 72 73 while (selection.Find.Execute()) //如果找到指定字符串返回真,否则返回false 74 { 75 selection.InsertBreak(7);//插入分页符,分页符常量为7,具体可查word api 76 } 77 } 78 79 wordObj .Application.Visible = true; 80 //word .saveAs("c:\\ba.doc"); //存放到指定的位置注意路径一定要是“\\”不然会报错 81 wordObj.ActiveWindow.ActivePane.View.Type=3;//设置浏览模式:1--为普通视图,2---大纲视图,3--页面视图, 4---web视图预览模式,5---大纲视图,6---web视图,7---阅读板式 82 wordObj.Application.close; 83 wordObj=null; 84 } 85 86 function outputExcel(elementID){ 87 var sel = document.body.createTextRange(); 88 var obj = document.getElementById(elementID); 89 sel.moveToElementText(obj); 90 sel.select(); 91 sel.execCommand("Copy"); 92 93 try{ 94 var excelObj = new ActiveXObject("Excel.Application"); //创建Word应用程序对象wordObj 95 }catch(e) 96 { 97 alert("无法调用Office对象,请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中!"); 98 return; 99 } 100 var excel = excelObj.Workbooks.Add();//新建excel表 101 var sheet = excel.ActiveSheet;//设置新加表为当前表 102 sheet.Paste(); 103 sheet.Cells.NumberFormatLocal = "@"; //设置单元格格式 104 sheet.Columns("A:D").Select;//选中第A列到第D列,如果只选D列为D:D 105 excelObj.Selection.ColumnWidth = 50;//设置选中列的宽度 106 sheet.Rows("1:5").Select;//选中第1行到第5行,如果只选2列为2 107 excelObj.Selection.RowHeight = 50;//设置选中行的高度 108 excelObj.Visible = true; 109 sheet = null; 110 excel = null; 111 excelObj = null; 112 } 113 </script> 114 <body> 115 <div id="prints"> 116 <table width="100%" border="1" id="PrintA"> 117 <tr><td>dfsdfds</td><td>&dsfds</td><td>&sdf;</td><td>dsf</td></tr> 118 <tr><td>dsf</td><td>dsfds</td><td>fsdfdsfdsf</td><td>dsfs</td></tr> 119 <tr><td>dsfds</td><td>dsf</td><td>dsfd</td><td>sfds</td></tr> 120 <tr><td> </td><td>sdf</td><td>fdsf</td><td> </td></tr> 121 </table> 122 nextpage 123 <table width="100%" border="1" id="PrintA"> 124 <tr><td>dfsdfds</td><td>&dsfds</td><td>&sdf;</td><td>dsf</td></tr> 125 <tr><td>dsf</td><td>dsfds</td><td>fsdfdsfdsf</td><td>dsfs</td></tr> 126 <tr><td>dsfds</td><td>dsf</td><td>dsfd</td><td>sfds</td></tr> 127 <tr><td> </td><td>sdf</td><td>fdsf</td><td> </td></tr> 128 </table> 129 <input type="button" onclick="javascript:outputWord(‘prints‘,‘‘,‘‘);" value="导出页面指定区域内容到Word"></input> 130 <input type="button" onclick="javascript:outputExcel(‘prints‘);" value="导出页面指定区域内容到Excel"></input> 131 </div> 132 </body> 133 </html>
时间: 2024-11-05 23:32:19