Office文檔在線編輯的一個實現方法
更新時間:2010年06月19日 17:52:10 作者:
因為項目的關系,研究了一下Office的在線編輯功能,寫出來共享一下。
Office xp之后的版本支持通過webdav協(xié)議(http的擴展)直接編輯服務器上的文件。
IIS(6.0)支持webdav,這在IIS管理器的web服務擴展中可以看到.利用IIS作為webdav的服務器端,可以很容易的實現office(word,excel等)的在線編輯.
可以簡單的實驗一下:
確保IIS的webdav擴展安裝并被啟用了,建立一個虛擬目錄test,在其中放一個word文檔a.doc,然后打開word, 文件->打開->輸入word文檔的訪問url(http://localhost/test/a.doc),
修改一下文檔內容,保存一下,發(fā)生了什么? 文檔被保存到服務器上了.
在IE中,可以通過js創(chuàng)建Word.Application,來打開,修改服務器上的文檔.
wApp = new ActiveXObject("Word.Application.11");
wApp.Visible = true ;
wApp.Documents.Open( url );
if( trackRevisions ){ //可以實現痕跡保留呢
wApp.ActiveDocument.TrackRevisions = true ;
wApp.ActiveDocument.ShowRevisions = false ;
}else
{
wApp.ActiveDocument.TrackRevisions = false ;
wApp.ActiveDocument.ShowRevisions = false ;
}
wApp.ActiveDocument.Application.UserName= Global_CurrentUserName;
另外,安裝office時,會同時按裝一個ActiveX組件:Sharepoint.OpenDocuments,可么用此組件來激活word,編輯服務器上的文檔: var __OpenDocuments = null ;
function Document_Edit2( url )
{
if( __OpenDocuments == null )
{
try{
__OpenDocuments = new ActiveXObject("SharePoint.OpenDocuments.3"); //for office 2007
}catch(e){}
if( __OpenDocuments == null || typeof(__OpenDocuments) == "#ff0000" )
{
try{
__OpenDocuments = new ActiveXObject("SharePoint.OpenDocuments.2"); //for office 2003
}catch(e){}
}
if( __OpenDocuments == null || typeof(__OpenDocuments) == "undefined" )
{
alert( "請安裝Word(2003或更高版本)" );
return ;
}
}
// openDocObj.ViewDocument("http://www.abc.com/documents/sample.doc");, "Word.Document"
//openDocObj.CreateNewDocument("http://www.abc.com/documents/sampleTemplate.dot", "http://www.abc.com/documents/");
var result = __OpenDocuments.EditDocument( url , "Word.Document" );
if( result == false )
{
alert( "無法打開文檔." );
}
}
可以看到,基于IIS的webdav支持,可以非常簡單的實現office文檔的在線編輯, 但有一個問題:這樣,文檔是存放在文件系統(tǒng)上,我們很多系統(tǒng)中,
文檔是存放在數據庫中的,這樣一來,如何實現呢???
I tried a lot and found the solution. It will be in the next article .
IIS(6.0)支持webdav,這在IIS管理器的web服務擴展中可以看到.利用IIS作為webdav的服務器端,可以很容易的實現office(word,excel等)的在線編輯.
可以簡單的實驗一下:
確保IIS的webdav擴展安裝并被啟用了,建立一個虛擬目錄test,在其中放一個word文檔a.doc,然后打開word, 文件->打開->輸入word文檔的訪問url(http://localhost/test/a.doc),
修改一下文檔內容,保存一下,發(fā)生了什么? 文檔被保存到服務器上了.
在IE中,可以通過js創(chuàng)建Word.Application,來打開,修改服務器上的文檔.
復制代碼 代碼如下:
wApp = new ActiveXObject("Word.Application.11");
wApp.Visible = true ;
wApp.Documents.Open( url );
if( trackRevisions ){ //可以實現痕跡保留呢
wApp.ActiveDocument.TrackRevisions = true ;
wApp.ActiveDocument.ShowRevisions = false ;
}else
{
wApp.ActiveDocument.TrackRevisions = false ;
wApp.ActiveDocument.ShowRevisions = false ;
}
wApp.ActiveDocument.Application.UserName= Global_CurrentUserName;
另外,安裝office時,會同時按裝一個ActiveX組件:Sharepoint.OpenDocuments,可么用此組件來激活word,編輯服務器上的文檔: var __OpenDocuments = null ;
復制代碼 代碼如下:
function Document_Edit2( url )
{
if( __OpenDocuments == null )
{
try{
__OpenDocuments = new ActiveXObject("SharePoint.OpenDocuments.3"); //for office 2007
}catch(e){}
if( __OpenDocuments == null || typeof(__OpenDocuments) == "#ff0000" )
{
try{
__OpenDocuments = new ActiveXObject("SharePoint.OpenDocuments.2"); //for office 2003
}catch(e){}
}
if( __OpenDocuments == null || typeof(__OpenDocuments) == "undefined" )
{
alert( "請安裝Word(2003或更高版本)" );
return ;
}
}
// openDocObj.ViewDocument("http://www.abc.com/documents/sample.doc");, "Word.Document"
//openDocObj.CreateNewDocument("http://www.abc.com/documents/sampleTemplate.dot", "http://www.abc.com/documents/");
var result = __OpenDocuments.EditDocument( url , "Word.Document" );
if( result == false )
{
alert( "無法打開文檔." );
}
}
可以看到,基于IIS的webdav支持,可以非常簡單的實現office文檔的在線編輯, 但有一個問題:這樣,文檔是存放在文件系統(tǒng)上,我們很多系統(tǒng)中,
文檔是存放在數據庫中的,這樣一來,如何實現呢???
I tried a lot and found the solution. It will be in the next article .
相關文章
Ewebeditor及fckeditork單引號問題的解決方法
為什么一個簡單的單引號會引發(fā)不能添加到數據庫呢,想到這里,我們想到了分析下入庫代碼并找出了原因,下面是解決方法。2010-04-04解決SyntaxHighlighter 代碼高亮不換行問題的解決方法
用SyntaxHighlighter 語法高亮插件的朋友可能都遇到過代碼顯示不換行的問題,這個問題在網上也找不到什么解決辦法,一直困擾了我很久,今天算是把它解決了,辦法其實簡單,下面說下如何解決2014-11-11