• DSOFramerControl简单使用


      以前大学做项目(Web的毕业论系统)就看见过在网页中嵌入Office的word,那时候用了哪个控件当时没记下来,我倒是忘了,最近发现在WinForm要嵌入Office的话,可以使用DSOFramerControl。

    环境

      使用之前要注册一下COM组件才行,使用 regsvr32注册 dsoframer.ocx。要是用这个的话,系统里面肯定要有装Office,要是很好使用的话,还得有Office开发组件,获得这组件的途径可以是在安装Office时把PIA也安装上。在这里可以找到添加上工具箱

    当然这篇文章中只涉及到Word的使用,这个控件其实能给Office的其余成员——Excel,Visio和PowerPoint使用。

    外观

    控件运行起来就好比把一整个Word的窗口嵌在了当前开发的窗体中。

    控件也大致包含了一个正常WinForm的基本元素,标题栏,工具栏,菜单栏。不过我用的是Office2013,它的标题栏和工具栏外观比较特殊,采用选项卡形式,导致控件的菜单栏只有一个File按钮。下面是网上看的到别的版本的Office的效果,菜单栏是齐的。

    若要对标题栏,工具栏,菜单栏这些元素的显示状态进行更改,可设置以下几个属性

    • Titlebar
    • Menubar
    • Toolbar

    这三个属性都是布尔类型,光看名字就知道是设那个栏了。

    基本文件操作

    对一个Office的文件一般操作包括打开,关闭,保存。而打开可分为读写打开还有只读打开;保存又可分成保存和另存为。

    打开可以调用Open方法,声明如下

            public virtual void Open(object document);
            public virtual void Open(object document, object readOnly, object progId, object webUsername, object webPassword);

    Document参数是一个object类型,一般可以传一个文件的全名过去

    this.officeWord.Open(@"C:Users居士Desktop emp博文系统特殊路径一览.docx");

    如果要只读打开的话,可以使用重载的方法,第二个参数传个False进去,打开的文档虽然可以编辑,但是无法保存的。这个涉及到保存那个操作了,延后说。

    关闭文档则调用

    public virtual void Close();

    还有一种关闭则是涉及到Microsoft.Office.Interop.Word.DocumentClass

    这个对象的,在过往使用中,曾经在关闭的时候没处理好,导致word文档一直处于被锁状态,每次打开只能以只读方式打开。于是我会在窗体或者控件的Dispose中调用这么写道

                    Microsoft.Office.Interop.Word.Document doc = this.officeWord.ActiveDocument as Microsoft.Office.Interop.Word.Document;
                    object missObj=null;
                    doc.Close(ref missObj,ref missObj,ref missObj);

    或者能把上面这段代码封装一下,然后在Form_Closing或者Form_Closed调用。

    保存的方法声明如下

            public virtual void Save();
            public virtual void Save(object saveAsDocument, object overwriteExisting, object webUsername, object webPassword);

    一般保存的话可以直接调用Save()方法,但是如果是在控件的菜单栏里新建的文档,该文档还不是一个物理文件时,调用Save()方法则会引发异常。

    利用DocumentClass保存。DocumentClass实现了Document接口,Documenet有以下几个方法

    1 void Save();
    2 void SaveAs(ref object FileName = Type.Missing, ref object FileFormat = Type.Missing, ref object LockComments = Type.Missing, ref object Password = Type.Missing, ref object AddToRecentFiles = Type.Missing, ref object WritePassword = Type.Missing, ref object ReadOnlyRecommended = Type.Missing, ref object EmbedTrueTypeFonts = Type.Missing, ref object SaveNativePictureFormat = Type.Missing, ref object SaveFormsData = Type.Missing, ref object SaveAsAOCELetter = Type.Missing, ref object Encoding = Type.Missing, ref object InsertLineBreaks = Type.Missing, ref object AllowSubstitutions = Type.Missing, ref object LineEnding = Type.Missing, ref object AddBiDiMarks = Type.Missing);
    3 
    4 void SaveAs2(ref object FileName = Type.Missing, ref object FileFormat = Type.Missing, ref object LockComments = Type.Missing, ref object Password = Type.Missing, ref object AddToRecentFiles = Type.Missing, ref object WritePassword = Type.Missing, ref object ReadOnlyRecommended = Type.Missing, ref object EmbedTrueTypeFonts = Type.Missing, ref object SaveNativePictureFormat = Type.Missing, ref object SaveFormsData = Type.Missing, ref object SaveAsAOCELetter = Type.Missing, ref object Encoding = Type.Missing, ref object InsertLineBreaks = Type.Missing, ref object AllowSubstitutions = Type.Missing, ref object LineEnding = Type.Missing, ref object AddBiDiMarks = Type.Missing, ref object CompatibilityMode = Type.Missing);
    5 
    6 void SaveAs2000(ref object FileName = Type.Missing, ref object FileFormat = Type.Missing, ref object LockComments = Type.Missing, ref object Password = Type.Missing, ref object AddToRecentFiles = Type.Missing, ref object WritePassword = Type.Missing, ref object ReadOnlyRecommended = Type.Missing, ref object EmbedTrueTypeFonts = Type.Missing, ref object SaveNativePictureFormat = Type.Missing, ref object SaveFormsData = Type.Missing, ref object SaveAsAOCELetter = Type.Missing);

    Save()就用在直接保存的,如果文档不存在,则会弹出“另存为”的文件保存对话框。SaveAs是用于另存为的。至于SaveAs2和SaveAs2000则是对应着docx和doc两种不同格式的文件。

      上面提及到的文档只读问题。如果以只读方式打开的话,调用AxFramerControl的Save方法是会抛异常的。如果用重载的话,第二个参数overwriteExisting并且第一个参数文件名是原本打开的文件的话,是可以保存成功的。如果利用Document接口,调用Save()则会弹出另存为的则会弹出一个“另存为”的文件保存对话框。如果调用的是SaveAs,文件名是原本打开的文件的话,也是可以覆盖保存的。

      这里提到的利用AxFramerControl的ActiveDocument属性,它是一个Object类型的,它可以转成DocumentClass类型,也可以转成DocumentClass2类型。能用哪种类型还取决于用户的系统上装了哪个版本的Office,如果装了Office 2007或以后版本的则可以使用DocumentClass和DocumentClass2;如果装了Office 2007以前的版本的话,那只能用DocumentClass了。

      下面内容则涉及到对Word文档内容的编辑,这里觉得有部分概念要先提一下。

    • 关于换行:Word文档的换行符其实是“ ”,这有别于平常的程序开发里面用到的“ ”或者“ ”。它识别不了“ ”这个字符,如果在文字编辑时用了“ ”则会显示别的字符上去。
    • 在Word里面是以Word(单词或中文的词)作为一个文字的基本单元,平时在编辑文档时看到的红色波浪下划线就是Word里面识别到这个word有错误才打上去的,一个或者多个的word可以组成Range,这个Range会在文字替换成图片的地方用到。

      不过下面部分的内容都是Microsoft.Office.Interop.Word.Document接口的使用

    替换文字

    替换文字的方法可定义如下,其中oMissing可以是一个object类型的字段

            public void Replace(Document doc,string oldString, string newString)
            {
                doc.Content.Find.Text = oldString;
                object FindText, ReplaceWith, ReplaceAll;
    
                FindText = oldString;
                ReplaceWith = newString;
                ReplaceAll = word.WdReplace.wdReplaceAll;
                doc.Content.Find.Execute(ref FindText,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref ReplaceWith,
                                          ref ReplaceAll,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing,
                                          ref oMissing);
    
            }

    查找内容

    查找内容的方法就用到Range这个类了。方法是我自己写的,比较菜,而且只适用于查找第一个匹配的内容,往后匹配的都被忽略掉了。其实改改还是可以实现查询所有匹配内容的。

            public Range FindRange(Document doc, string text)
            {
                Range range;
                object start,end;
                for (int i = 0; true; i++)
                { 
                    start=i;end=i+text.Length;
                    try{
                        range = doc.Range(ref start, ref end);
                        if (range.Text == text) return range;
                    }
                    catch (Exception ex) { break; }
                }
    
                return null;
            }

    插入图片

    插入图片要也要用到Range,其实上面的查找方法算是为调用这个方法而定义的。至于调整图片的样式,目前还没有去考究

            private void AddImage(Document doc, string imageFile,Range range)
            {
                string fileName = imageFile;  //要插入的图片
                Object oLinkToFile = false;  //缺省
                Object oSaveWithDocument = true;// 缺省
                doc.InlineShapes.AddPicture(fileName, ref  oLinkToFile, ref  oSaveWithDocument, ref  oMissed);
            }

    此外在网上也找了一份DSOFramerControl的API文档,附带一下

     dso_api.rar

    以上有什么说错的说漏的欢迎大家批评指出,谢谢!

  • 相关阅读:
    u-boot器件驱动模型(Device&Drivers)之uclass (转)
    u-boot下的DM驱动模型 阶梯状 (转)
    u-boot-2018.09 DTS上 I2C节点的解析 (转)
    [uboot] (番外篇)uboot串口&console&stdio设备工作流程 (转)
    [uboot] (番外篇)uboot 驱动模型(转)重要
    u-boot DM初始化流程
    【u-boot】u-boot中initf_dm()函数执行流程(转)
    【u-boot】u-boot对设备树的节点解析(转)
    BeanPostProcessor
    一些压力测试结果(Mysql,Zookeeper,Redis,Mongodb)
  • 原文地址:https://www.cnblogs.com/HopeGi/p/3464686.html
Copyright © 2020-2023  润新知