web中,如何读取客户端word内容并且显示在页面中? 我发现有2种方法,一种是直接在客户端操作,一种是把客户端的word上传到服务器端操作,这2种各有优缺点。我现就举出客户端操作的例子首先在.aspx中添加如下js代码 <script language='javascript'> //默认word转化文件放于c://下var os__localpath = "c://";//保存的文件名 var os__localfile = "defaultfileword.htm"; var os__xmldom = new activexobject("msxml2.domdocument"); var os__xmlfso ; //保存数据到当前客户端(可以传入一个要保存的文件名). function os_savetolocal() { var _saveas = ""; if(arguments.length > 0) _saveas = arguments[0] + ""; else _saveas = os__localfile; try { if(os__xmlfso == null) os__xmlfso = new activexobject("scripting.filesystemobject"); } catch(e){window.alert(e);} } //word转化为html文件 function worcchangehtml() { var os_xmlfso; //获得上传控件对象 var objupfile = window.document.form1.updfile; //获得客户端word文件路径和文件 var upfilevalue = window.document.form1.updfile.value; if(os__xmlfso == null) os__xmlfso = new activexobject("scripting.filesystemobject"); try { if(window.document.form1.updfile.value == "") { alert('请选择对应的word文件'); objupfile.focus(); } else if(upfilevalue.indexof(".doc") == -1) { alert('您选择的不是word文件 /r/n请选择正确的word文件'); objupfile.focus(); } else if(!os__xmlfso.fileexists(objupfile.value)) { alert('对应的word文件不存在'); objupfile.focus(); } else { var wdformathtml = 8; var objword = new activexobject("word.application"); objword.application.visible = false; var objdoc = objword.documents.open(upfilevalue); objdoc.saveas(os__localpath+os__localfile, wdformathtml); window.document.form1.updfile.value = ""; objdoc.close(); objword.quit(); var gethtml = getline(); var ibeginindex = gethtml.indexof("<body"); var iendindex = gethtml.lastindexof("</body>"); gethtml = gethtml.substring(ibeginindex,iendindex+7).replace("<body","<div"); gethtml = gethtml.replace("</body>","</div>"); //将转化后的值赋给页面控件txtidea的值,我为了将word值保存进数据库所以用<input type = "hidden" ..... 如果将word内容显示可以考虑 window.document.form1."你的显示控件id".innertext = gethtml; window.document.form1.txtidea.value = gethtml; } } catch(e){window.alert(e);} } //读取文本文件 function getline() { var fso, txtfile, strvalue; var forreading = 1, forwriting = 2; fso = new activexobject("scripting.filesystemobject"); txtfile = fso.opentextfile(os__localpath+os__localfile, forreading); while(!txtfile.atendofstream) { strvalue = strvalue + txtfile.readline(); } txtfile.close(); return(strvalue); } </script> 注意页面上需要添加以下2个控件和对应的客户端事件 <input id="updfile" type="file" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; width: 77.46%; border-bottom: 1px solid; height: 26px" size="71"> <input style="border-right: #999999 1px solid; border-top: #999999 1px solid; font-size: 15pt; border-left: #999999 1px solid; width: 103px; border-bottom: #999999 1px solid; height: 28px" οnclick="worcchangehtml()" runat="server" id="btnupload" type="submit" value="导入" name="btnupload"> <textarea style="width: 15.25%; height: 23px" rows="50" cols="16" id="txtidea" runat="server"> 其中txtidea中的值就是客户端word中的内容了,注意:需要调整ie的安全性设置,否则将无效
读取客户端word内容并且显示在页面
最新推荐文章于 2023-03-22 15:20:17 发布