C#:使用dsoframer.ocx控件實現內嵌office效果(詳解)


1. 問題描述
最近在研究“如何實現在桌面程序中,實現內嵌office的功能,能夠對辦公文件(Excel/ Word/ PPT/ Visio)實現常見的查看、編輯、保存等功能”的問題。在思考實現的技術思路時,接觸到了關聯的ActiveX控件:dsoframer.ocx。於是,看了許多文章,趟過了一些坑,此處總結了一個簡單的demo。希望能給需要的人以參考。

開發背景: vs2017+.net framework 4.6 + C#
關聯控件: dsoframer.ocx(下載鏈接)(https://download.csdn.net/download/qq_34769196/13239121)
本次Demo源代碼下載: DsoFramerOffice.rar(https://download.csdn.net/download/qq_34769196/13239154)

2. 效果展示
實現功能:

注冊dsoframer.ocx控件。
取消注冊dsoframer.ocx控件。
加載 ppt / excel / word 等office常用文件類型。
使用同一個AxDSOFramer.AxFramerControl類對象,在不同文件類型的文件間進行切換。
圖片如下:

 

 

 

 

 

 

 

 

 


3. 步驟描述
注意: 需要本地安裝了Office,dsoframer.ocx控件實質上是控制本地安裝好的office來編輯文件,是一種在線編輯的形態。

不僅僅可以應用在桌面程序上,web網頁上調用ActiveX控件也能達到同樣的效果。
如果由需要,可以自行研究。

具體步驟:(總共就3個步驟)

注冊“dsoframer.ocx”。
打開vs2017,項目中添加Com組件。
查看API文件,編寫代碼調用相關接口,實現所需功能。
尤其注意:

在vs中添加COM組件時,如果沒有注冊dsoframer.ocx,無法正常將其添加到工具箱,從而無法正常使用。

添加過程如下:
1.在工具箱中,點擊右鍵菜單,彈出項中點擊“選擇項”。

 

 


2.在彈出窗口中,選擇“Com組件” 選擇框,勾選“DSO Framer Control Object”,點擊確定。

注意:一定要注冊dsoframer.ocx后,才能在添加com組件時,找到"DSO Framer Control Object"。

 

 


3.然后在工具箱中,成功添加了“DSO Framer Control Object”控件,可像按鈕一樣拖拽使用。

 

 


4. 關鍵步驟代碼
注冊dsoframer.ocx的部分代碼:

/// <summary>
/// 注冊dsoframer.ocx
/// </summary>
/// <returns>返回-1:注冊失敗;返回1:注冊成功;返回2:已注冊過,不用重復注冊</returns>
public static int RegisterMain()
{
// 存放ocx文件的目錄: bin\Debug\ocx\dsoframer.ocx
string fileFullPath = string.Format("{0}\\ocx\\dsoframer.ocx", System.Environment.CurrentDirectory);
string systemDrive = System.Environment.GetEnvironmentVariable("systemdrive");//獲取系統所在的盤符
// 確保dsoframer.ocx存在
if (!File.Exists(fileFullPath) || String.IsNullOrEmpty(systemDrive))
return -1; // dsoframer.ocx不存在,注冊失敗
bool isRegisted = IsRegistered("00460182-9E5E-11D5-B7C8-B8269041DD57");
if (isRegisted)
return 2; // 已注冊過,注冊成功

string windowsPath = string.Empty;
if (GetOSBitCount() == "64")
windowsPath = string.Format("{0}\\Windows\\SysWOW64", systemDrive);
else
windowsPath = string.Format("{0}\\Windows\\System32", systemDrive);
if (!Directory.Exists(windowsPath))
return -1;//目標目錄不存在,注冊失敗
// 復制dsoframer.ocx文件到C:\\Windows\\SysWOW64 或 C:\\Windows\\System32
File.Copy(fileFullPath, string.Format("{0}\\dsoframer.ocx", windowsPath), true);

bool result = Registe(string.Format("{0}\\dsoframer.ocx", windowsPath)); //開始注冊

if (result)
{
return -1; //注冊失敗
}
return 1;//注冊成功
}
//注冊dsoframer.ocx
private static bool Registe(string fileFullName)
{
bool result = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");//注冊完畢不顯示是否成功的提示
//System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName);//注冊完畢顯示是否成功的提示
if (p != null && p.HasExited)
{
Int32 exitCode = p.ExitCode;
if (exitCode == 0)
result = true;
}
return result;//成功時,返回false

取消注冊dsoframer.ocx的部分代碼:

//取消注冊dsoframer.ocx
private static bool UnRegiste(string fileFullName)
{
bool result = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /u");//取消注冊
if (p != null && p.HasExited)
{
Int32 exitCode = p.ExitCode;
if (exitCode == 0)
result = true;
}
return result;//成功時,返回false

判斷dsoframer.ocx是否已經注冊的代碼:

調用過程bool isRegisted = IsRegistered("00460182-9E5E-11D5-B7C8-B8269041DD57");
//判斷控件是否已經注冊
private static bool IsRegistered(String CLSID)
{
if (String.IsNullOrEmpty(CLSID))
return false;

String key = String.Format(@"CLSID\{{{0}}}", CLSID);
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);
if (regKey != null)
return true;
else
return false;

5. 舉兩個例子
至於如何創建AxDSOFramer.AxFramerControl對象,並調用屬性方法實現具體功能,需要再研究具體的API文檔。(此處不再詳述)

1.隱藏AxDSOFramer.AxFramerControl對象的“標題欄、菜單欄、工具欄”,對應隱藏office的“標題欄、菜單欄、工具欄”。

private AxDSOFramer.AxFramerControl m_axFramerControl;
... ...
m_axFramerControl.Titlebar = false;//是否顯示excel標題欄
m_axFramerControl.Menubar = false;//是否顯示excel的菜單欄
m_axFramerControl.Toolbars = false;//是否顯示excel的工具欄
 
2.初始化office控件,加載 Excel/ Word/ PPT/ Visio

/// <summary>
/// 初始化office控件,加載Excel/Word/PPT
/// </summary>
/// <param name="_sFilePath"></param>
private void InitOfficeControl(string _sFilePath)
{
try
{
if (m_axFramerControl == null)
{
throw new ApplicationException("請先初始化office控件對象!");
}

//this.m_axFramerControl.SetMenuDisplay(48);
//這個方法很特別,一個組合菜單控制方法,我還沒有找到參數的規律,有興趣的朋友可以研究一下
string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".", "");
//this.m_axFramerControl.CreateNew(this.LoadOpenFileType(sExt));//創建新的文件
this.m_axFramerControl.Open(_sFilePath, false, this.LoadOpenFileType(sExt), "", "");//打開文件
//隱藏標題
this.m_axFramerControl.Titlebar = false;
}
catch (Exception ex)
{
throw ex;
}
}

//下面這個方法是dso打開文件時需要的一個參數,代表office文件類型
/// <summary>
/// 根據后綴名得到打開方式
/// </summary>
/// <param name="_sExten"></param>
/// <returns></returns>
private string LoadOpenFileType(string _sExten)
{
try
{
string sOpenType = "";
switch (_sExten.ToLower())
{
case "xls":
sOpenType = "Excel.Sheet";
break;
case "xlsx":
sOpenType = "Excel.Sheet";
break;
case "doc":
sOpenType = "Word.Document";
break;
case "docx":
sOpenType = "Word.Document";
break;
case "ppt":
sOpenType = "PowerPoint.Show";
break;
case "pptx":
sOpenType = "PowerPoint.Show";
break;
case "vsd":
sOpenType = "Visio.Drawing";
break;
default:
sOpenType = "Word.Document";
break;
}
return sOpenType;

}
catch (Exception ex)
{
throw ex;
}

6. 總結
此次demo實現功能點不多,但可以幫助入門借助dsoframer.ocx控件實現在線編輯office功能的研究,能基本完成在winform桌面程序中,內嵌office辦公程序的效果。
具體代碼下載入口: DsoFramerOffice.rar(https://download.csdn.net/download/qq_34769196/13239154)

 

 

 

————————————————
版權聲明:本文為CSDN博主「XY的技術筆記」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_34769196/article/details/110511741


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM