控件附帶說明給出了保存文檔至服務(wù)器的javascript函數(shù),如:
function SaveToWeb()
{
document.all.FramerControl1.HttpInit();
document.all.FramerControl1.HttpAddPostCurrFile("FileData", "mydoc.doc");
var err = document.all.FramerControl1.HttpPost("if (!err)
alert('保存失??!');
else
alert('保存成功!');
}
由此可見,關(guān)鍵問題是如何實(shí)現(xiàn)SaveDoc.aspx模塊。于是乎在網(wǎng)上搜索相應(yīng)的解決方案,但沒有一個(gè)能在服務(wù)器上成功保存正確的文件。失望之余索性將原文檔和上傳文檔用UltraEdit打開進(jìn)行二進(jìn)制級(jí)比較,然后抓包分析POST數(shù)據(jù)時(shí)http數(shù)據(jù)包的格式,最后終于找到了解決的辦法,貼出來供遇到同樣問題的朋友參考,代碼如下:
BinaryReader bReader = new BinaryReader(Request.InputStream);
string strTemp = Encoding.GetEncoding("iso-8859-1").GetString(
bReader.ReadBytes((int)bReader.BaseStream.Length), 0, (int)bReader.BaseStream.Length);
string match = "Content-Type: application/msword\r\n\r\n";
int pos = strTemp.IndexOf(match) + match.Length;
bReader.BaseStream.Seek(pos, SeekOrigin.Begin);
string newFile = Server.MapPath(".") + "\\MyFile2.doc";
FileStream newDoc = new FileStream(newFile, FileMode.Create, FileAccess.Write);
BinaryWriter bWriter = new BinaryWriter(newDoc);
bWriter.BaseStream.Seek(0, SeekOrigin.End);
while (bReader.BaseStream.Position < bReader.BaseStream.Length - 38)
bWriter.Write(bReader.ReadByte());
bReader.Close();
bWriter.Flush();
bWriter.Close();
這里應(yīng)該注意的是,從字節(jié)流中獲取字符串時(shí)一定要采用iso-8859-1的編碼方式,不要采用utf-8或其他,因?yàn)閡tf-8會(huì)將asci字符也擴(kuò)展成相應(yīng)的unicode雙字節(jié)形式。原理很簡單,代碼面前了無秘密。
DSOFramer原有的接口說明
===================================================================
DSOFramer原有的接口說明
1.void CreateNew(BSTR ProgIdOrTemplate)
新建文檔,
其中: ProgIdOrTemplate參數(shù):
Excel Spreadsheet "Excel.Sheet"
Excel Chart "Excel.Chart"
PowerPoint Presentation "PowerPoint.Show"
Project Project "MSProject.Project"
Visio Drawing "Visio.Drawing"
Word Document "Word.Document"
2. HRESULT Open([in] VARIANT Document, [in, optional] VARIANT ReadOnly,
[in, optional] VARIANT ProgId, [in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword)
打開文檔,可以是本地文件或者是服務(wù)器文件
參數(shù):
Document 文檔路徑
ReadOnly 是否已只讀模式打開
ProgId OLE類型
WebUsername 用戶名(訪問網(wǎng)絡(luò)的文件時(shí)候,有可能需要)
WebPassword 密碼
例子:
DsoFramer1.Open "C:\TestBook.xls"
DsoFramer1.Open "C:\Plain.txt", , "Word.Document" //用Word來打開c:\plain.txt文件
DsoFramer1.Open "https://secureserver/test/mytest.asp?id=123", True, "Excel.Sheet", "MyUserAccount", "MyPassword"
3.HRESULT Save([in, optional] VARIANT SaveAsDocument, [in, optional] VARIANT OverwriteExisting,
[in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword);
保存文件在本地
DsoFramer1.Save "c:\1.doc"
4.Activate
激活當(dāng)前文檔,沒搞明白有什么用
5. HRESULT ActiveDocument([out,retval] IDispatch** ppdisp);
返回當(dāng)前活動(dòng)文檔的Dispatch接口,這個(gè)接口很重要,可以通過這個(gè)接口,操作所有的文檔接口。
如:下面 javascript 語句調(diào)用Office內(nèi)置的對(duì)話框
var obj;
obj = new Object(document.all.FramerControl1.ActiveDocument);
if(obj !=null){
var dd;
dd = obj.Application.Dialogs(84).Show();
//... ...
//delete it
delete obj;
}
6. HRESULT Close();
關(guān)閉當(dāng)前文檔,建議在頁面關(guān)閉的時(shí)候調(diào)用。
MS的原來的版本,有時(shí)候關(guān)不掉Word,已經(jīng)修復(fù)了。
7. HRESULT Caption([out,retval] BSTR* pbstr);
屬性,獲取|設(shè)置窗口標(biāo)題
8. HRESULT Titlebar([in] boolean vbool);
HRESULT Titlebar([out,retval] boolean* pbool);
顯示或者隱藏標(biāo)題欄
9. HRESULT Toolbars([in] boolean vbool);
HRESULT Toolbars([out,retval] boolean* pbool);
顯示或者隱藏工具欄
10. HRESULT ModalState([in] boolean vbool);
HRESULT ModalState([out,retval] boolean* pbool);
11.HRESULT ShowDialog([in] dsoShowDialogType DlgType);
顯示對(duì)話框
12.HRESULT EnableFileCommand([in] dsoFileCommandType Item, [in] boolean vbool);
HRESULT EnableFileCommand([in] dsoFileCommandType Item, [out,retval] boolean* pbool);
13. HRESULT BorderStyle([in] dsoBorderStyle style);
HRESULT BorderStyle([out, retval] dsoBorderStyle* pstyle);
14. HRESULT BorderColor([in] OLE_COLOR clr);
HRESULT BorderColor([out,retval] OLE_COLOR* pclr);
15. HRESULT BackColor([in] OLE_COLOR clr);
HRESULT BackColor([out,retval] OLE_COLOR* pclr);
16.HRESULT ForeColor([in]OLE_COLOR clr);
HRESULT ForeColor([out,retval]OLE_COLOR* pclr);
17.HRESULT TitlebarColor([in] OLE_COLOR clr);
HRESULT TitlebarColor([out,retval] OLE_COLOR* pclr);
18.HRESULT TitlebarTextColor([in] OLE_COLOR clr);
HRESULT TitlebarTextColor([out,retval] OLE_COLOR* pclr);
19.HRESULT ExecOleCommand([in] LONG OLECMDID, [in, optional] VARIANT Options, [in, optional] VARIANT* vInParam, [in, out, optional] VARIANT* vInOutParam);
20.HRESULT Menubar([in] boolean vbool);
HRESULT Menubar([out,retval] boolean* pbool);
21.HRESULT HostName([in] BSTR bstr);
HRESULT HostName([out,retval] BSTR* pbstr);
22. HRESULT DocumentFullName([out,retval] BSTR* pbstr);
文檔的路徑
23.HRESULT PrintOut([in, optional] VARIANT PromptUser, [in, optional] VARIANT PrinterName, [in, optional] VARIANT Copies,
[in, optional] VARIANT FromPage, [in, optional] VARIANT ToPage, [in, optional] VARIANT OutputFile);
24.HRESULT PrintPreview();
25.HRESULT PrintPreviewExit();
26.HRESULT IsReadOnly([out,retval] boolean* pbool);
是否為只讀的。
27.HRESULT IsDirty([out,retval] boolean* pbool);
是否保存了,實(shí)際可以用來判讀文檔有沒有修改
oframer.IsDirty = TRUE //文檔沒有保存,處于修改狀態(tài)
oframer.IsDirty = FALSE //文檔已經(jīng)保存,沒有修改
新加的接口說明(開發(fā)接口)
[color=red]當(dāng)前版本:V2.2.0.8 2007-02-07
[/color]下載控件需要登錄
說明:
控件未經(jīng)大批量測試,難免有Bug,
發(fā)現(xiàn) Bug,請(qǐng)及時(shí)發(fā)帖或者M(jìn)ail:wanhhf@gmail.com
版本修改記錄:
V2.2.0.8修改:
增加了N多個(gè)事件,挺不錯(cuò)的東西
[id(DSOF_DISPID_WORD_DocumentChange), helpstring("DSOF_DISPID_WORD_DocumentChange")]
HRESULT WORD_DocumentChange();
[id(DSOF_DISPID_WORD_DocumentBeforePrint), helpstring("DSOF_DISPID_WORD_DocumentBeforePrint")]
HRESULT WORD_DocumentBeforePrint();
[id(DSOF_DISPID_WORD_WindowActivate), helpstring("DSOF_DISPID_WORD_WindowActivate")]
HRESULT WORD_WindowActivate();
[id(DSOF_DISPID_WORD_WindowSelectionChange), helpstring("DSOF_DISPID_WORD_WindowSelectionChange")]
HRESULT WORD_WindowSelectionChange();
[id(DSOF_DISPID_WORD_WindowBeforeRightClick), helpstring("DSOF_DISPID_WORD_WindowBeforeRightClick")]
HRESULT WORD_WindowBeforeRightClick();
[id(DSOF_DISPID_WORD_WindowBeforeDoubleClick), helpstring("DSOF_DISPID_WORD_WindowBeforeDoubleClick")]
HRESULT WORD_WindowBeforeDoubleClick();
V2.2.0.6修改:
修改Open,參數(shù)為空時(shí)候,一個(gè)小 Bug
修改了URL過長時(shí)候一個(gè)Bug
增加了一個(gè)替換文字的接口
long ReplaceText(BSTR strSearchText, BSTR strReplaceText, long lGradation);
V2.2.0.2修改:
修改了HttpPost相對(duì)路徑的一些問題。
V2.2.0.0增加:
[id(0x00010041), helpstring("Get Rev Index")]
HRESULT GetRevCount( [out,retval] long * pbool);
[id(0x00010042), helpstring("Get Rev Index Info")]
HRESULT GetRevInfo([in] long lIndex, [in] long lType, [out,retval] BSTR* pbool);
[id(0x00010043), helpstring("Set Doc Prop")]
HRESULT SetValue([in] BSTR strValue, [in] BSTR strName, [out,retval] long* pbool);
[id(0x00010044), helpstring("Set Doc Variable")]
HRESULT SetDocVariable([in] BSTR strVarName, [in] BSTR strValue,[in] long lOpt, [out,retval] long* pbool);
[id(0x00010045), helpstring("Save page To Doc")]
HRESULT SetPageAs([in] BSTR strLocalFile, [in] long lPageNum, [in] long lType,[out,retval] long* pbool);
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
LoadDso.js
var s = ""
s += "<OBJECT id=DSOFramer align='center' style='LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%'"
s += "classid=clsid:00460182-9E5E-11D5-B7C8-B8269041DD57 codeBase=DSOFramer.ocx#Version=2,2,0,6' >"
s += "</OBJECT>"
document.write(s)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
接口文檔:
/*
1.新建
*/
//新建Word
document.all.FramerControl1.CreateNew("Word.Document");
//新建Excel
document.all.FramerControl1.CreateNew("Excel.Sheet");
/*
2.打開文件
*/
//打開制定的本地文件
document.all.FramerControl1.Open("C:\\TestBook.xls");
//制定用Word來打開c:\plain.txt文件
document.all.FramerControl1.Open("C:\\Plain.txt",false, "Word.Document");
//打開服務(wù)器的文件
document.all.FramerControl1.Open "https://secureserver/test/mytest.asp?id=123",true, "Excel.Sheet", "MyUserAccount", "MyPassword");
//打開服務(wù)器的文件
document.all.FramerControl1.Open("http://localhost/1.doc", true);
/*
3.保存文件
*/
//到本地
document.all.FramerControl1.Save("c:\\1.doc",true);
//服務(wù)器
/*增加Http協(xié)議Post上傳接口,可以Post一個(gè)動(dòng)態(tài)頁面(jsp,asp,php...),由動(dòng)態(tài)頁面負(fù)責(zé)解析數(shù)據(jù)
bool HttpInit();
bool HttpAddPostString(BSTR strName, BSTR strValue);
bool HttpAddPostCurrFile(BSTR strFileID, BSTR strFileName);
BSTR HttpPost(BSTR bstr);
*/
//初始化Http引擎
document.all.FramerControl1.HttpInit();
//增加Post變量
document.all.FramerControl1.HttpAddPostString("RecordID","20060102200");
document.all.FramerControl1.HttpAddPostString("UserID","李局長");
//上傳打開的文件
document.all.FramerControl1.HttpAddPostCurrFile("FileData", "文檔名.doc");
//執(zhí)行上傳動(dòng)作
document.all.FramerControl1.HttpPost("/*
4.修訂留痕
*/
//進(jìn)入留痕狀態(tài)
document.all.FramerControl1.SetTrackRevisions(1);
//進(jìn)入非留痕狀態(tài)
document.all.FramerControl1.SetTrackRevisions(0);
//接受當(dāng)前修訂
document.all.FramerControl1.SetTrackRevisions(4);
/*
5.設(shè)置當(dāng)前用戶
*/
document.all.FramerControl1.SetCurrUserName("張三");
/*
6.設(shè)置當(dāng)前時(shí)間(筆跡留痕會(huì)顯示("Like 2006:02:07 11:11:11")
*/
document.all.FramerControl1.SetCurrTime("2006:02:07 11:11:11");
/*
7.設(shè)置和創(chuàng)建書簽,此功能比較強(qiáng)大,設(shè)置書簽數(shù)據(jù)、添加書簽和添加紅頭文件就靠他了
SetFieldValue(BSTR strFieldName, BSTR strValue, BSTR strCmdOrSheetName)
strFieldName:書簽名
strValue:要設(shè)置的值
strCmdOrSheetName:
命令
::ADDMARK:: 添加BookMark
::DELMARK:: 刪除這個(gè)BookMark
::GETMARK:: 定位到這個(gè)BookMark
::FILE:: 插入的是文件
::JPG:: 插入的是圖片
一般來說:WORD中書簽是做好的,可以通過此接口把外界數(shù)據(jù)設(shè)置進(jìn)書簽中去。
*/
//在當(dāng)前WORD位置插入標(biāo)簽,標(biāo)簽名為"book1",數(shù)值為"test"
document.all.FramerControl1.SetFieldValue("book1","test","::ADDMARK::");
//設(shè)置書簽"Time",數(shù)值為"2006-03-16 22:22:22"
document.all.FramerControl1.SetFieldValue("Time","2006-03-16 22:22:22","");
//在書簽位置"hongtou",插入紅頭文件"document.all.FramerControl1.SetFieldValue("hongtou","/*
8.設(shè)置菜單顯示情況
BOOL SetMenuDisplay(long lMenuFlag)
lMenuFlag為以下數(shù)值的組合
#define MNU_NEW 0x01
#define MNU_OPEN 0x02
#define MNU_CLOSE 0x04
#define MNU_SAVE 0x08
#define MNU_SAVEAS 0x16
#define MNU_PGSETUP 0x64
#define MNU_PRINT 0x256
#define MNU_PROPS 0x32
#define MNU_PRINTPV 0x126
*/
//只有“新建”菜單可用
document.all.FramerControl1..SetMenuDisplay(1);
//只有“打開”菜單可用
document.all.FramerControl1.SetMenuDisplay(2);
//只有“打開”和“新建”菜單可用
document.all.FramerControl1.SetMenuDisplay(3);
/*