Word,excel开发指南

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  
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script language="javascript">
function outputWord(elementID,HorV,pagekey){
var sel = document.body.createTextRange(); //var sel = object.createTextRange()创建可操作区域,用来在内从中编辑,sel.select()为全选,sel.execCommand("Copy");执行复制命令
var obj = document.getElementById(elementID);//获取网页对象
sel.moveToElementText(obj); //将sel区域选中到obj对象结尾
sel.select();
sel.execCommand("Copy"); //执行复制命令,将内容放入粘贴板
try{
var wordObj = new ActiveXObject("Word.Application"); //创建Word应用程序对象wordObj
}catch(e)
{
alert("无法调用Office对象,请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中!");
return;
}
var word = wordObj.Documents.Add("",0,1);//不打开模版直接加入内容,创建空白Word文档
//var word = wordObj.documents.open("c:\\ba.doc"); //此处为打开已有的模版,打开已存word文档,会把新内容插入文档最前端
if (HorV!="")
{
//横向打印设置部分
word.PageSetup.LineNumbering.Active = false;
word.PageSetup.Orientation. wdOrientLandscape = 1; //设置为横向打印
word.PageSetup.TopMargin = word.Application.CentimetersToPoints(3.17);
word.PageSetup.BottomMargin = word.Application.CentimetersToPoints(3.17);
word.PageSetup.LeftMargin = word.Application.CentimetersToPoints(2.54);
word.PageSetup.RightMargin = word.Application.CentimetersToPoints(2.54);
word.PageSetup.Gutter = word.Application.CentimetersToPoints(0);
word.PageSetup.HeaderDistance = word.Application.CentimetersToPoints(1.5);
word.PageSetup.FooterDistance = word.Application.CentimetersToPoints(1.75);
word.PageSetup.PageWidth = word.Application.CentimetersToPoints(29.7);
word.PageSetup.PageHeight = word.Application.CentimetersToPoints(21);
word.PageSetup.FirstPageTray = 0;
word.PageSetup.OtherPagesTray = 0;
word.PageSetup.SectionStart = 2;
word.PageSetup.OddAndEvenPagesHeaderFooter = false;
word.PageSetup.DifferentFirstPageHeaderFooter = false;
word.PageSetup.VerticalAlignment = 0;
word.PageSetup.SuppressEndnotes = false;
word.PageSetup.MirrorMargins = false;
word.PageSetup.TwoPagesOnOne = false;
word.PageSetup.BookFoldPrinting = false;
word.PageSetup.BookFoldRevPrinting = false;
word.PageSetup.BookFoldPrintingSheets = 1
word.PageSetup.GutterPos = 0;
word.PageSetup.LayoutMode = 2;
}
word.ShowGrammaticalErrors = false; //屏蔽语法检查
word.ShowSpellingErrors = false; //屏蔽拼写错误
//var oRange = word.Range(0,1);//在word中创建连续区域,打开文档的区域,选中0到1的字符
//oRange.Paste(); //粘贴1:将粘贴板内容存入word选中区域中
wordObj.Selection.Paste();//粘贴2:或者直接粘贴到新建的word中 if (pagekey!="")
{
var selection = wordObj.Selection;//得到打开后word的selection对象
//selection.Font.Size = 10; //设置字体大小
selection.WholeStory(); //ctrl+A 全选操作
selection.Find.ClearFormatting(); //清除格式
selection.Find.Text = pagekey; //指定查找关键字
selection.Find.Forward = true; //向下查找
selection.Find.Wrap = 1;//向下查找
selection.Find.MatchCase = false; //不区分大小写
selection.Find.MatchWholeWord = false; //不匹配整个单词 while (selection.Find.Execute()) //如果找到指定字符串返回真,否则返回false
{
selection.InsertBreak(7);//插入分页符,分页符常量为7,具体可查word api
}
} wordObj .Application.Visible = true;
//word .saveAs("c:\\ba.doc"); //存放到指定的位置注意路径一定要是“\\”不然会报错
wordObj.ActiveWindow.ActivePane.View.Type=3;//设置浏览模式:1--为普通视图,2---大纲视图,3--页面视图, 4---web视图预览模式,5---大纲视图,6---web视图,7---阅读板式
wordObj.Application.close;
wordObj=null;
} function outputExcel(elementID){
var sel = document.body.createTextRange();
var obj = document.getElementById(elementID);
sel.moveToElementText(obj);
sel.select();
sel.execCommand("Copy"); try{
var excelObj = new ActiveXObject("Excel.Application"); //创建Word应用程序对象wordObj
}catch(e)
{
alert("无法调用Office对象,请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中!");
return;
}
var excel = excelObj.Workbooks.Add();//新建excel表
var sheet = excel.ActiveSheet;//设置新加表为当前表
sheet.Paste();
sheet.Cells.NumberFormatLocal = "@"; //设置单元格格式
sheet.Columns("A:D").Select;//选中第A列到第D列,如果只选D列为D:D
excelObj.Selection.ColumnWidth = 50;//设置选中列的宽度
sheet.Rows("1:5").Select;//选中第1行到第5行,如果只选2列为2
excelObj.Selection.RowHeight = 50;//设置选中行的高度
excelObj.Visible = true;
sheet = null;
excel = null;
excelObj = null;
}
</script>
<body>
<div id="prints">
<table width="100%" border="1" id="PrintA">
<tr><td>dfsdfds</td><td>&dsfds</td><td>&sdf;</td><td>dsf</td></tr>
<tr><td>dsf</td><td>dsfds</td><td>fsdfdsfdsf</td><td>dsfs</td></tr>
<tr><td>dsfds</td><td>dsf</td><td>dsfd</td><td>sfds</td></tr>
<tr><td>&nbsp;</td><td>sdf</td><td>fdsf</td><td>&nbsp;</td></tr>
</table>
nextpage
<table width="100%" border="1" id="PrintA">
<tr><td>dfsdfds</td><td>&dsfds</td><td>&sdf;</td><td>dsf</td></tr>
<tr><td>dsf</td><td>dsfds</td><td>fsdfdsfdsf</td><td>dsfs</td></tr>
<tr><td>dsfds</td><td>dsf</td><td>dsfd</td><td>sfds</td></tr>
<tr><td>&nbsp;</td><td>sdf</td><td>fdsf</td><td>&nbsp;</td></tr>
</table>
<input type="button" onclick="javascript:outputWord('prints','','');" value="导出页面指定区域内容到Word"></input>
<input type="button" onclick="javascript:outputExcel('prints');" value="导出页面指定区域内容到Excel"></input>
</div>
</body>
</html>
上一篇:Vue基础开发笔记


下一篇:Socket网络编程-UDP编程