js导出到word文档模板(2010-04-06 13:27:48)
转载标签:it
1.保存html页面到word
<HTML>
<HEAD>
<title>
</title>
</HEAD>
<body>
<form id="form">
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" >
<TR style="text-align : center;">
<TD>单元格1</TD>
<TD>单元格2</TD>
<TD>单元格3</TD>
<TD>单元格4</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="red" face="Verdana">单元格合并</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "Test" width="100%">
<tr>
<td><font color="red">test</FONT></td>
</tr>
</table>
</form>
<input type="button" onclick="javascript:MakeWord();" value="导出页面到Word">
<SCRIPT LANGUAGE="javascript">
function MakeWord()
{
var word = new ActiveXObject("Word.Application");
// var doc = word .documents.open("c:\test.doc"); //此处为打开已有的模版
var doc = word .Documents.Add("",0,1);//不打开模版直接加入内容
var Range=doc.Range();
var sel = document.body.createTextRange();
sel.moveToElementText(form);//此处form是页面form的id
sel.select();
sel.execCommand("Copy");
Range.Paste();
word .Application.Visible = true;
alert("s");
word.Application.Selection.InlineShapes.AddPicture("c:\\m20.gif");
alert("n");
doc .saveAs("c:\\ba.doc"); //存放到指定的位置注意路径一定要是“\\”不然会报错
}
</SCRIPT>
</body>
</html>
***************************************************************************************************
2.用JS生成WORD
***************************************************************************************************
<script>
function wordcontorl(){
alert("1111")
var WordApp=new ActiveXObject("Word.Application");
var wdCharacter=1
var wdOrientLandscape = 1
WordApp.Application.Visible=true;
var myDoc=WordApp.Documents.Add();
WordApp.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
WordApp. Selection.ParagraphFormat.Alignment=1 //1居中对齐,0为居右
WordApp. Selection.Font.Bold=true
WordApp. Selection.Font.Size=20
WordApp. Selection.TypeText("我的标题");
WordApp. Selection.MoveRight(wdCharacter); //光标右移字符
WordApp.Selection.TypeParagraph() //插入段落
WordApp. Selection.Font.Size=12
WordApp. Selection.TypeText("副标题"); //分行插入日期
WordApp.Selection.TypeParagraph() //插入段落
var myTable=myDoc.Tables.Add (WordApp.Selection.Range, 8,7) //8行7列的表格
//myTable.Style="网格型"
var aa = "我的列标题"
var TableRange; //以下为给表格中的单元格赋值
for (i= 0;i<7;i++)
{
with (myTable.Cell(1,i+1).Range)
{
font.Size = 12;
InsertAfter(aa);
ColumnWidth =4
}
}
for (i =0;i<7;i++)
{
for (n =0;n<7 ;n++)
{
with (myTable.Cell(i+2,n+1).Range)
{font.Size = 12;
InsertAfter("bbbb");
}
}
}
row_count = 0;
col_count = 0
myDoc.Protect(1)
}
wordcontorl()
</script>
***************************************************************************************************
3.遍历导出到word
***************************************************************************************************
1、遍历导出每个文本框内的内容。
<script language="javascript">
function OpenWord(){//导出word
var txt="txt";
for(i=0;i<table1.rows.length;i++)//遍历导出图表和文字
{
txt="txt"+jilu[i];
myRange =mydoc.Range(myRange.End-1,myRange.End);//设定起始点
var sel=Layer1.document.body.createTextRange();
//sel.moveToElementText(table1);
sel.moveToElementText(document.all[txt]);
sel.select();
Layer1.document.execCommand('Copy');
sel.moveEnd('character');
myRange.Paste();
myRange =mydoc.Range(myRange.End-1,myRange.End);
myRange.InsertAfter("\n");
ExcelSheet.ActiveWindow.View.TableGridlines = false;//隐藏虚框
}
}
</script>
2、拷贝table1内的内容到word
<script language="javascript">
function OpenWord(){//导出word
Layer1.style.border=0;
ExcelSheet = new ActiveXObject('Word.Application');
ExcelSheet.Application.Visible = true;
var mydoc=ExcelSheet.Documents.Add('',0,0);
myRange =mydoc.Range(0,1);
myRange =mydoc.Range(myRange.End-1,myRange.End);//设定起始点
var sel=Layer1.document.body.createTextRange();
sel.moveToElementText(table1);
sel.select();
Layer1.document.execCommand('Copy');
sel.moveEnd('character');
myRange.Paste();
myRange =mydoc.Range(myRange.End-1,myRange.End);
myRange.InsertAfter("\n");
ExcelSheet.ActiveWindow.View.TableGridlines = false;
}
</script>