用javascript操作word文档
1、 向word中写入内容
首先在word中设置书签,如书签名为bookmark,javascript中可以这样写
var word;
word = new ActiveXObject("Word.Application");
var range = word.Range;
word.Visible = true;
var path = "filepath";
word.Documents.Open(path);
range = word.ActiveDocument.Bookmarks("bookmark").Range;
range.InsertBefore("哈哈哈哈哈哈");
2、把word文件转成html文件
<script language=javascript>
function saveword(){
var oWordApp=new ActiveXObject("Word.Application");
var oDocument=oWordApp.Documents.Open("C://doc2html//x.doc");
oDocument.SaveAs("C://test.htm", 8)
oWordApp.Quit();
}
</script>
</HEAD>
<BODY>
Click the "save" button to save the file "C:/test.doc" to "C:/test.htm":
<input type=button οnclick="saveword()" value=save>
</BODY>
</HTML>
3。 将表格导出到excel
<script language="javascript">
function tableToExcel() {
window.clipboardData.setData("Text",document.all('theObjTable').outerHTML);
try
{
var ExApp = new ActiveXObject("Excel.Application")
var ExWBk = ExApp.workbooks.add()
var ExWSh = ExWBk.worksheets(1)
ExApp.DisplayAlerts = false
ExApp.visible = true
}
catch(e)
{
alert("您的电脑没有安装Microsoft Excel软件!")
return false
}
ExWBk.worksheets(1).Paste;
}
</script>
<table id=theObjTable>
<tr>
<td>a</td>
<td>b</td>
</tr>
</table>
<input type="button" value="导入到excel" οnclick="tableToExcel()">