<html>
<head>
<script type="text/javascript">
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
function word(id,way){
var sel = document.body.createTextRange();
sel.moveToElementText(id);
sel.select();
sel.execCommand(way);
oRange.Paste();
}
function test(){
var table=document.getElementById('table1');
var table_cells = table.rows[0].cells;
var form_elements = document.getElementById('huahai');
word(div_content,'Copy'); //调用word函数,将div_content范围内容拷贝到word里面。
for(i=0;i<table_cells.length;i++){
oRange =oDC.Range(oRange.End-1,oRange.End); //设定位置依次由上往下、从左往右
var sel = document.body.createTextRange();
sel.moveToElementText(table_cells[i]); //将单元格内容复制到word
sel.select();
sel.execCommand("Copy");
sel.moveEnd('character'); //不加这句导出不了,里面参数为character、不是copy
oRange.Paste();
oRange =oDC.Range(oRange.End-1,oRange.End);
}
oRange =oDC.Range(oRange.End-1,oRange.End); //复制不同的东西,需要写这句继续 写死的这句话就是位置
var img = document.getElementById('img');
word(img,'Copy');//将img范围内容拷贝到word里面。
oRange =oDC.Range(oRange.End-1,oRange.End);
var text_area = document.getElementById('text_area');
word(text_area,'Copy');//将text_area范围内容拷贝到word里面。
oRange =oDC.Range(oRange.End-1,oRange.End);
oWD.Application.Visible = true; //这句意思是所有操作完毕后,在显示出来,如果写在里面,会发现word打开后,什么标签啊、内容啊就跟打字机一样往里面填
}
</script>
</head>
<body>
<form action="" id="huahai" >
<div align="center">
<div align="center" id="div_content">
<h2>
<font color="red">测试导出word</font>
</h2>
<h4>
<font color="red">测试导出word</font>
</h4>
</div>
<table id="table1">
<tr>
<td>姓名</td><td><input type="text" size="5"></td>
<td>年龄</td><td><input type="text" size="5"></td>
</tr>
<table>
<div id="img">
<hr/>
<img src="MM.jpg" height="45%" width="30%">
</br>
</div>
<div id="text_area">
<textarea name="warn_task" wrap="off" cols="80" rows="12">区域内容:</textarea>
</textarea>
<hr />
</div>
<input type="button" onclick="javascript:test();" value="测试">
</div>
</form>
</body>
</html>