<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'); //調(diào)用word函數(shù),將div_content范圍內(nèi)容拷貝到word里面。
for(i=0;i<table_cells.length;i++){
oRange =oDC.Range(oRange.End-1,oRange.End); //設(shè)定位置依次由上往下、從左往右
var sel = document.body.createTextRange();
sel.moveToElementText(table_cells[i]); //將單元格內(nèi)容復制到word
sel.select();
sel.execCommand("Copy");
sel.moveEnd('character'); //不加這句導出不了,里面參數(shù)為character、不是copy
oRange.Paste();
oRange =oDC.Range(oRange.End-1,oRange.End);
}
oRange =oDC.Range(oRange.End-1,oRange.End); //復制不同的東西,需要寫這句繼續(xù) 寫死的這句話就是位置
var img = document.getElementById('img');
word(img,'Copy');//將img范圍內(nèi)容拷貝到word里面。
oRange =oDC.Range(oRange.End-1,oRange.End);
var text_area = document.getElementById('text_area');
word(text_area,'Copy');//將text_area范圍內(nèi)容拷貝到word里面。
oRange =oDC.Range(oRange.End-1,oRange.End);
oWD.Application.Visible = true; //這句意思是所有操作完畢后,在顯示出來,如果寫在里面,會發(fā)現(xiàn)word打開后,什么標簽啊、內(nèi)容啊就跟打字機一樣往里面填
}
</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">區(qū)域內(nèi)容:</textarea>
</textarea>
<hr />
</div>
<input type="button" onclick="javascript:test();" value="測試">
</div>
</form>
</body>
</html>