不要说JavaScript了,连java都难以做到。
因为word文档是微软自己设置特定格式的文档。它不像txt是纯文本的,也不像pdf是开源的,它是商业机密。
所以目前能够操作word文档的程序很少,apache有一个POI的项目,关于word的部分也不得不停做了。
因为word文档是微软自己设置特定格式的文档。它不像txt是纯文本的,也不像pdf是开源的,它是商业机密。
所以目前能够操作word文档的程序很少,apache有一个POI的项目,关于word的部分也不得不停做了。
调试欢乐多
例子如下:
1、先在C盘根目录建立 test.doc (模拟下载)
2、在 test.doc 中加入 #test# (将被替换的内容。如果有多个关键词,就调用多次 replaceString 方法)
3、运行下面的代码
4、上传,并删除本地文件(这一步不知道怎么实现)<script>
var cntOperation_Replace = 1;
var cntOperation_Paste = 2;
function toWord(){
//打开文件
var wdApp = null;
try{
wdApp = new ActiveXObject("Word.Application");
wdApp.Application.Visible =1;
var wdDoc=wdApp.Documents.Open("c:\\test.doc");// 绝对路径
wdApp.Application.ActiveWindow.WindowState=2;
}catch(e){
alert("打开文件出错:"+e.message);
return false;
}
//替换内容
try{
var testcontent="替换后的内容";
replaceString(cntOperation_Replace, wdApp, "#test#",testcontent);
}catch(e){
alert("替换文件内容出错:"+e.message);
return false;
}finally{
wdApp = null;
wdDoc = null;
}
}/*
功能: 将标记替换为指定文本
intOperation: 1-替换指定文本 2-插入(粘贴)正文
App: 文档对象
strLabel: 需要进行替换的标记
strValue: 替换内容
*/
function replaceString(intOperation, App, strLabel, strValue){
try{
App.Selection.Find.Text = strLabel;
App.Selection.Find.Execute();
App.Selection.Find.Forward = true;
App.Selection.Find.Wrap = 2;
App.Selection.Find.MatchByte = true;
if(App.Selection.Find.Found){
App.Selection.Delete();
if (intOperation==cntOperation_Replace){
// 替换指定文本
App.Selection.InsertAfter(strValue);
}else{
// 粘贴正文
App.Selection.Paste();
}
}
}catch(e){
alert("替换文件内容出错!出错描述:"+e.message);
return false;
}
}toWord();
</script>
你需要捕捉文件的关闭或保存事件,然后做上传的动作。2楼你用的是vbscript,javascript几乎不可能完成这个任务啊
另,wdApp.Documents.Open("c:\\test.doc");就不能Open一个url吗?我懒得试了
如果谁有好主意我也想看看。
var cntOperation_Replace = 1;
var cntOperation_Paste = 2;
var filepath = "c:\\aaa\\"// 存放到哪个文件夹
var strUrl = "./test.doc";// URL路径,可以是绝对
var filename = "aaa.doc";// 要保存的名称
function toWord(){
try{
var objFile = new ActiveXObject("Scripting.FileSystemObject");
if(objFile == null){
alert("您的机器不支持Scripting.FileSystemObject.");
return false;
}
if(!objFile.FolderExists(filepath))
objFile.CreateFolder(filepath);//创建存放的临时文件
}catch(e){
alert("创建FileSystemObject对象出错!"+e.message);
return false;
} try{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if(xmlhttp == null){
alert("您的机器不支持Microsoft.XMLHTTP.");
return false;
}
xmlhttp.open("Get", strUrl,false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
}catch(e){
alert("创建XMLHTTP对象出错!"+e.message);
return false;
}
var ado_stream = null;
// 文件存盘
try{
ado_stream = new ActiveXObject("ADODB.Stream");
if(ado_stream == null){
alert("您的机器不支持ADODB.Stream.");
return false;
}
ado_stream.Type = 1;
ado_stream.open();
ado_stream.Write(xmlhttp.responseBody);
ado_stream.SaveToFile(filepath + filename,2);
}catch(e){
alert("文件存盘出错:"+e.message);
return false;
}finally{
ado_stream.close();
ado_stream = null;
} //打开文件
var wdApp = null;
try{
wdApp = new ActiveXObject("Word.Application");
wdApp.Application.Visible =1;
var wdDoc=wdApp.Documents.Open(filepath + filename);
wdApp.Application.ActiveWindow.WindowState=2;
}catch(e){
alert("打开文件出错:"+e.message);
return false;
}
//替换内容
try{
var testcontent="替换后的内容";
replaceString(cntOperation_Replace, wdApp, "#test#",testcontent);
}catch(e){
alert("替换文件内容出错:"+e.message);
return false;
}finally{
wdApp = null;
wdDoc = null;
}
}/*
功能: 将标记替换为指定文本
intOperation: 1-替换指定文本 2-插入(粘贴)正文
App: 文档对象
strLabel: 需要进行替换的标记
strValue: 替换内容
*/
function replaceString(intOperation, App, strLabel, strValue){
try{
App.Selection.Find.Text = strLabel;
App.Selection.Find.Execute();
App.Selection.Find.Forward = true;
App.Selection.Find.Wrap = 2;
App.Selection.Find.MatchByte = true;
if(App.Selection.Find.Found){
App.Selection.Delete();
if (intOperation==cntOperation_Replace){
// 替换指定文本
App.Selection.InsertAfter(strValue);
}else{
// 粘贴正文
App.Selection.Paste();
}
}
}catch(e){
alert("替换文件内容出错!出错描述:"+e.message);
return false;
}
}toWord();
</script>
1.对权限太敏感了,随意下载文件有中毒的风险,随意上传文件有泄漏隐私的风险,一般计算机的浏览器都会严格限制这两种操作(特别是上传,很容易造就第二个灌稀哥)。
2.怎么检测文件关闭呢?
3.ActiveXObject是依赖于IE的永远都不可能跨浏览器。
最后还是建议你用.NET做客户端去实现,.NET操作Office组件很好很强大,文件的上传下载可以用WebDAV,不黄也不暴力>_<
我现在可以下载和修改保存了,多谢 sd5816690