将所传的DIV或者table输出(拷贝)到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" οnclick="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(PrintA); //此处PrintA是页面上DIV的id
- sel.select();
- sel.execCommand("Copy");
- Range.Paste();
- word.Application.Visible = true;
- //存放到指定的位置注意路径一定要是“//”不然会报错
- doc.saveAs("c://ba.doc");
- }
- </SCRIPT>
- </body>
- </html>