在做OA或者工作流程的网站中,常常能够看到一些在线Word编辑进行文档处理的功能,这里我开发了一个在线Word编辑插件并且以此为例。
1.NTKO Word在线编辑器介绍:
NTKO的官方网站:,pdf。filetype参数保存文件类型 3.这里要注意几个问题,首先在保存文档到远程服务器的时候,可以使用aspx页面或者ashx页面去处理。 具体调用的代码:jqObj.save("process.aspx?action=test&data=demo", { formId: "formeditor" }); 在第一次使用该插件的时候,首先必须进行安装,您可以通过下载一个Word编辑器插件安装包.rar包,来安装插件,包里面包含相关的批处理操作,大家自己可以试试。
this.formSettings.FileName,//上传文件的名称,类似
);
returnretHTML;
},
//下载远程文件
downloadFile:function(url,localPath){
this[0].DownloadFile(url,localPath);
},
//插入本地文档
insertLocalFile:function(){
this[0].ActiveDocument.Application.Dialogs(164).Show();
},
//插入文档
insertFile:function(url){
this._doc=this[0].ActiveDocument;
this._doc.Activate();
//this._doc.Select();
//this._doc.Application.Selection.Select();
this[0].AddTemplateFromURL(url);
},
//插入本地图片
insertLocalImage:function(){
this[0].ActiveDocument.Application.Dialogs(163).Show();
},
//插入图片
insertImage:function(url,type){
this[0].InsertFile(url,type+8);
},
//插入文本
insertText:function(text){
this._doc=this[0].ActiveDocument;
this._doc.Activate();
this._doc.Application.Selection.InsertAfter(text);
},
//页面设置
setPageSetup:function(){
this[0].showdialog(this.showDialogType.PageSetup);
},
//文档属性设置
setProperty:function(){
this[0].showdialog(this.showDialogType.Properties);
},
//打印
print:function(){
//this[0].PrintOut();
this[0].showdialog(this.showDialogType.Print);
},
//打印预览
printPreview:function(){
this[0].PrintPreview();
},
//关闭打印预览
printExit:function(){
this[0].PrintPreviewExit();
},
//隐藏工具栏
hideToolbar:function(){
this[0].Activate(false);
this[0].Toolbars=false;
},
//显示工具栏
showToolbar:function(){
this[0].Activate(false);
this[0].Toolbars=true;
},
//隐藏菜单栏
hideMenubar:function(){
this[0].Activate(false);
this[0].Menubar=false;
},
//显示菜单栏
showMenubar:function(){
this[0].Activate(false);
this[0].Menubar=true;
},
//获取或设置窗口高度
height:function(arg){
if(typeof(arg)!=="undefined"){
this.css("height",arg);
}
else{
returnthis.css("height");;
}
},
//获取或设置窗口宽度
width:function(arg){
if(typeof(arg)!=="undefined"){
this.css("width",arg);
}
else{
returnthis.css("width");;
}
},
//删除本地路径,如c:\\1.doc
deleteLocalFile:function(localPath){
this[0].DeleteLocalFile(localPath);
},
showView:function(showViewType){
this[0].Activate(false);
this[0].ActiveDocument.ActiveWindow.View=showViewType;
},
//添加本地附加文件
//addLocalFile:function(fileID,locaPath){
//this[0].HttpAddPostFile(fileID,locaPath);
//},
//hostName:function(){
//returnthis[0].HostName;
//},
//documentFullName:function(){
//returnthis[0].DocumentFullName;
//},
//caption:function(){
//returnthis[0].Caption;
//},
//track:function(){
//this[0].SetCurrUserName("liping");
//this[0].SetTrackRevisions(1);
//},
//showTrack:function(){
//this[0].ShowRevisions(1);
//this[0].ShowView(0);
//},
//是否只读
isReadOnly:function(){
returnthis[0].IsReadOnly;
},
//文档是否已做修改
isDirty:function(){
return!this[0].ActiveDocument.Saved;
},
//获取文档所有内容,返回该Word文档内容Json
range:function(){
this._doc=this[0].ActiveDocument;
this._doc.Activate();
this._range=this._doc.Range();
this.wordInfo.Start=this._range.Start;
this.wordInfo.End=this._range.End;
this.wordInfo.Text=this._doc.Range(this._range.Start,this._range.End).Text;
returnthis.wordInfo;
},
//选中文档所有内容,返回该Word文档内容Json
select:function(){
this._doc=this[0].ActiveDocument;
this._doc.Activate();
this._doc.Select();
this._selection=this._doc.Application.Selection;
this.wordInfo.Start=this._selection.Start;
this.wordInfo.End=this._selection.End;
this.wordInfo.Text=this._doc.Range(this._selection.Start,this._selection.End).Text;
returnthis.wordInfo;
},
//取消选中内容
unselect:function(){
if(this._selection!=null){
this._selection.Start=0;
this._selection.End=0;
this._selection=null;
}
},
replace:function(text,start,end){
//alert(this._doc.Range(start,end).Text);
this._doc.Range(start,end).Text=text;
//alert(this._doc.range(this.wordInfo.Starti,this.wordInfo.End).Text);
},
//清除文本
clear:function(){
varwordDoc=this[0].ActiveDocument;
wordDoc.Activate();
wordDoc.Select();
varwordSelection=wordDoc.Application.Selection;
wordDoc.Range(wordSelection.Start,wordSelection.End).Text="";
}
});
$.extend($.ntko,
{
settings:
{
folder:"",
width:"700px",
height:"500px",
id:"OFRAMER_"+newDate().getTime(),
isToolbar:true,
isMenubar:true,
failed:function(){}
},
init:function(obj,parameters){
//赋值参数
if(typeof(parameters.folder)!=="undefined")
this.settings.folder=parameters.folder;
if(typeof(parameters.width)!=="undefined")
this.settings.width=parameters.width;
if(typeof(parameters.height)!=="undefined")
this.settings.height=parameters.height;
if(typeof(parameters.id)!=="undefined")
this.settings.id=parameters.id;
if(typeof(parameters.isToolbar)!=="undefined")
this.settings.isToolbar=paOA系统rameters.isToolbar;
if(typeof(parameters.isMenubar)!=="undefined")
this.settings.isMenubar=parameters.isMenubar;
if(typeof(parameters.failed)!=="undefined")
this.settings.failed=parameters.failed||{};
//构造插件Html
this._createHTML(obj);
varacxId=this.settings.id;
varacxJQjery=null;
varfailed=this.settings.failed;
//判断是否安装了word编辑器插件
try{
document.getElementById(acxId).Close();
acxJQjery=$("#"+acxId);
}
catch(e){
failed(e);
}
returnacxJQjery;
},
//构造插件Html
_createHTML:function(obj){
$(obj).html(
this._stringFormat("
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"",
this.settings.folder,
this.settings.id,
this.settings.width,
this.settings.height,
this.settings.isToolbar+""=="true"?"1":"0",
this.settings.isMenubar+""=="true"?"1":"0"
));
},
//string格式化构造器
_stringFormat:function(str){
varargs=arguments;
returnstr.replace(/\{(\d+)\}/g,
function(m,i){
returnargs[parseInt(i)+1];
});
}
}
);
}
)(jQuery);
可以发现我这里有个formId的参数,这个是因为我必须把Word插件必须放在Form标签中,这样在POST数据,也就是文件流的时候有用。
而在process.aspx页面中,的具体代码实现为:其中,一定要记得写Response.ContentEncoding = Encoding.GetEncoding("GB2312");这段代码,如果没有写,返回的中文会出现乱码的情况。我们通过HttpFileCollection files = Request.Files;方式可以取得一个POST文件流。
{
Response.ContentEncoding=Encoding.GetEncoding("GB2312");
stringaction=Request.QueryString["aapp软件开发ction"]??"";
switch(action)
{
case"test":
Test();//测试保存数据
break;
}
}
privatevoidTest()
{
stringdata=Request.QueryString["data"]??"";
HttpFileCollectionfiles=Request.Files;
try
{
if(files.Count>0)
{
HttpPostedFilefile=files[0];
stringfileName=Server.MapPath(String.Format("~/output/{0}_{1}.doc",data,Guid.NewGuid()));
file.SaveAs(fileName);
Response.Write(String.Format("保存附件“{0}”成功!\r\n返回测试数据:{1}",fileName,data));
}
}
catch
{
Response.Write("有异常");
}
}