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重新整理)

猜你喜欢

转载自www.cnblogs.com/lhxsoft/p/9146677.html