ActiveX控件dsoFramer的使用(word、excel、PPT)


  dsoframer是微軟提供一款開源的用於在線編輯、調用Word、 Excel 、PowerPoint等的ActiveX控件。國內很多著名的OA中間件,電子印章,簽名留痕等大多數是依此改進而來的。

  一、現狀

    1.官方有Dsoframer 1.3,支持Office2003和2007,也有遠程保存的方法,但這個遠程保存方法要求遠程服務器的 HTTP 支持 Microsoft FrontPage Server Extensions (FPSE) 或 Web 分發創作和版本控制 (WebDAV) 協議擴展,有點不現實。
    2.網上廣為流傳的還有Dsoframer 2.2.1.2,該控件支持HttpPost遠程保存,但不支持Office 2007. 
    3.Dsoframer 2.3.0.0,這個是網上改的2.2.1.2版使其支持Office 2007

  下載地址:

    1. Dsoframer 1.3:http://download.microsoft.com/download/7/1/2/712086b9-20de-4bf8-967b-2ef4b5ae4f6f/DsoFramer_KB311765_x86.exe 
    2. Dsoframer 2.2.1.2:http://download.csdn.net/source/1232750
    3. Dsoframer 2.3.0.0:http://download.csdn.net/source/1375531

 

  二、使用

    在下載的過程中可能出現exe等等,不要着急,安裝一下就可以,得到的是一個后綴名為ocx的組件,那么,問題來了,怎么調用,怎么注冊。

    1、注冊(對於程序員來說,這不是問題,主要拷貝ocx組件到對應的目錄)。

      32位:regsvr32.exe c:\windows\system32\dsoframer.ocx

      64位:regsvr32.exe c:\windows\sysWOW64\dsoframer.ocx

    2、添加組件(Visual Studio 2013)

      首先,選擇項:

      

      然后選擇com組件:

      

      選擇了之后,點擊確定即可。

      

 

      添加了組件之后,放置到我們的窗體上,然后右擊屬性,哈哈,是不是發現了很多東西哦。

      

    3、基本操作

      dsoFramer的主要目的是操作word,excel等相關組件,目前我自己用的時候我覺得還是挺穩定的,還不錯吧,函數里面的基本信息,如Open(),Save(),Close()等等,諸如此類的,就不說了
      1、基本屬性:TitileBar、MenuBar、ToolBar,均可以在屬性欄設置隱藏。
      2、和普通組件一樣,可以有Visable、Dock、Anchor(設置邊緣)、broderStyle、Size、padding等一般控件的公有屬性。
      3、當我們添加了toolTip控件之后,該組件和其他控件一樣,有"tooltip上的ToolTip"這個屬性。

    4、系統設置

      1、添加這個組件之后,可能會有一個”Unable to dispaly the inactive document。Click here to Reactive the Document“的字樣,不要緊張,那是因為我們又一個屬性沒有設置,當前設置屬性的時候也一定要注意一點。在構造這個窗體的時候”axFramerControl.ActivationPolicy = DSOFramer.dsoActivationPolicy.dsoKeepUIActiveOnAppDeactive;“,這句話是必不可少的,當然,為了防止報錯,這句話我直接加了一個try{}catch{},因為當不需要打開文檔的時候,去構造這個屬性是不存在的。其實呢,還可以選擇控件屬性在ActivationPolicy通過下拉菜單選擇屬性為dsoKeepUIActiveOnAppDeactive。(注意,dsoFramer2.3以后的版本不會出現這個錯誤,這個是在引用wps的時候可能會出現的)
      2、添加了組件之后,可以把組件的Dock屬性設置為Fill,當然,這些其實和其他的控件是一樣的。
      注意:1、dsoFramer用的是不能同時存在一個線程里面的。也就是說可以單獨開一個窗口,再綁定一個dsoFramer。
           2、dsoFramer因為采用的內嵌辦公軟件,所以呢,快捷鍵全部支持。

  三、事件

    因為本身采用的是word或者wps之類的辦公軟件,所以,可以直接調用word的接口或者說wps的接口直接操作。

    1、注冊事件(當組件使用的時候,再去調用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/// <summary>
///
/// </summary>
private  void  RegisterInfo()
{
     Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document) this .axFramerControlMain.ActiveDocument;
     doc.Application.WindowSelectionChange +=  new  Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler( this .SelectEvent);
     doc.Application.WindowActivate +=  new  Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler( this .SettingInfomation);
}
 
/// <summary>
///
/// </summary>
/// <param name="doc"></param>
/// <param name="Wn"></param>
private  void  SettingInfomation(Microsoft.Office.Interop.Word.Document doc, Microsoft.Office.Interop.Word.Window Wn)
{
     doc = (Microsoft.Office.Interop.Word.Document) this .axFramerControlMain.ActiveDocument;
}
 
/// <summary>
///
/// </summary>
/// <param name="sel"></param>
private  void  SelectEvent(Microsoft.Office.Interop.Word.Selection sel)
{
     if  (sel.Text !=  null )
     {
         if  (Convert.ToString(sel.Text.Trim()) ==  "釋義" )
         {
             SettingWordInfo(240, 140, 150, 100,  "對可能造成投資者理解障礙及有特定含義的名稱縮寫、專有名詞等作出釋義,應當在目錄次頁排印。" );
         }
     }
}

     2、插入Shape對象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// <summary>
/// 文本域提示信息
/// <param name="left">距左</param>
/// <param name="top">距上</param>
/// <param name="width">提示框寬度</param>
/// <param name="height">提示框高度</param>
/// <param name="text">提示信息</param>
/// <returns>當前對象</returns>
/// </summary>
private  void  SettingWordInfo( int  left = 0,  int  top = 0,  int  width = 0,  int  height = 0,  string  text =  null )
{
     Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document) this .axFramerControlMain.ActiveDocument;
     Microsoft.Office.Interop.Word.Shape sp = doc.Shapes.AddShape(106,
         (left == 0) ? 100 : left, (top == 0) ? 20 : top, (width == 0) ? 100 : width, (height == 0) ? 30 : height,
         System.Reflection.Missing.Value);
     sp.TextFrame.TextRange.Text = (text ==  null ) ?  "提示信息!"  : text;
     sp.Shadow.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
     sp.Shadow.OffsetX = 10;
     sp.Shadow.OffsetY = 2;
}

     3、設置顯示的格式

1
2
3
4
5
6
7
8
9
10
11
/// <summary>
    /// 1、換行符不顯示
    /// 2、設置word格式為web視圖格式
    /// </summary>
    private  void  SettingWord()
    {
        Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document) this .axFramerControlMain.ActiveDocument;
        ((Microsoft.Office.Interop.Word.Application)doc.Parent).ActiveWindow.View.ShowSpaces =  true ; //設置換行符不顯示
        ((Microsoft.Office.Interop.Word.Application)doc.Parent).ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdWebView; //設置為web視圖
        RegisterInfo();
    }

     4、在dsoFramer的當前位置插入圖片(鼠標所在的位置)

復制代碼
/// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.png|*.png|*.jpg|*.jpg";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ImgFilePath = ofd.FileName.ToString();
                this.axFramerControlMain.Focus();
                //
                //插入圖片
                //
                Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)this.axFramerControlMain.ActiveDocument;
                ((Microsoft.Office.Interop.Word.Application)doc.Parent).Selection.InlineShapes.AddPicture(ImgFilePath, false, true,
                    ((Microsoft.Office.Interop.Word.Application)doc.Parent).ActiveDocument.Application.Selection.Range);
            }
        }
復制代碼

     5、讀取當前控件中的表格中的圖片信息

復制代碼
/// <summary>
        /// 獲取表格中的所有圖片信息
        /// </summary>
        public List<string> GetDsoImgList()
        {
            if (System.IO.Directory.Exists(Application.StartupPath + "//Imgtemp"))
            {
                System.IO.Directory.Delete(Application.StartupPath + "//Imgtemp",true);
                System.IO.Directory.CreateDirectory(Application.StartupPath + "//Imgtemp");
            }
            else
            {
                System.IO.Directory.CreateDirectory(Application.StartupPath + "//Imgtemp");
            }
            List<string> list = new List<string>();
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            object Nothing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Table table = null;
            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)this.axFramerControlMain.ActiveDocument;
            if (doc.Tables.Count == 0)
            {
                MessageBox.Show("該文檔不存在提取信息");
            }
            for (int k = 1; k < doc.Tables.Count + 1; k++)
            {
                table = doc.Tables[k];
                for (int i = 1; i < table.Rows.Count + 1; i++)//必須從1開始
                {
                    for (int j = 1; j < table.Columns.Count + 1; j++)//必須從1開始
                    {
                        var bmp = table.Cell(i, j).Range.InlineShapes;
                        if (bmp.Count > 0)
                        {
                            foreach (Microsoft.Office.Interop.Word.InlineShape shape in bmp)
                            {
                                //判斷類型
                                if (shape.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                                {
                                    //利用剪貼板保存數據
                                    shape.Select(); //選定當前圖片
                                    ((Microsoft.Office.Interop.Word.Application)doc.Parent).Selection.Copy();//復制這個圖片
                                    string fileName = "";
                                    if (Clipboard.ContainsImage())
                                    {
                                        Bitmap _bmp = new Bitmap(Clipboard.GetImage());
                                        fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
                                        var name =Application.StartupPath + "//Imgtemp//"+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
                                        _bmp.Save(name, System.Drawing.Imaging.ImageFormat.Png);
                                        list.Add(name);
                        _bmp.Dispose(); } } } } } } } return list; }
復制代碼

 

  OK,代碼提示就到這里,但是對於這個,我想真正看了的人都懂的,這個就是"Microsoft.Office.Interop.Word"這個對象調用,然后把dsoFramer的對象直接強轉為word對象來操作的,貌似應該瞬間明白了不少吧。

    本人親測:dsoFramer2.3以上支持office03-13(winfrom、cs方向),WPS完美兼容(winform、cs方向)

    對於用word對象替換文字的時候,要注意word對象的 app.Selection.Find.Replacement.Text的這個屬性,超過256個字符長度就會報錯,所以呢,需要對字符串處理一下,拆成已256為單位的字符串數據,然后處理。

 

    友情提示:

    word對象不了解的時候,用"宏",哪里有錄制宏,和查看宏,你想要的操作。先錄制一段宏出來,然后呢,看看就懂了,那個是VB的代碼,但是對象都是一樣的,所以我們看的不是代碼有多少,而是看的每一步的操作用的那個對象。

    

    友情鏈接:

    1、DSO(dsoframer)的接口文檔
    2、DSOframer 的簡單介紹和資源整理(2015-09-02重新整理)


免責聲明!

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



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