zoukankan      html  css  js  c++  java
  • 转 Word文档.net(c#)编辑公式(使用域 Fields)

    公式编辑器很容易编辑上面的公式,但是使用.net程序如何实现公式编辑?毕竟工程计算上需要使用很多复杂公式,用.net生成说明书使用规范的公式是必然的。
      

    Word文档 .net(c#)编辑公式 (使用域 Fields)

     

    公式编辑器很容易编辑上面的公式,但是使用.net程序如何实现公式编辑?毕竟工程计算上需要使用很多复杂公式,用.net生成说明书使用规范的公式是必然的。

       EqEquation(公式)的缩写,Eq域能够生成数学公式。创建公式当然最好用公式编辑器了,但在某些情况下使用Eq 域来输入简单的数学公式也是一个不错的选择。根据Eq域的不同开关,分别能完成以下内容:绘制二维数组、用方括号括住单个元素、创建分数、使用一个或两个元素绘制根号、设置上下标、在元素四周绘制边框及将多个值组成一个列表等等。

    域的表达式一般为:eq \开关\选择项(文字),它允许同时套用多种开关创建更复杂的公式

    首先看看Word菜单插入/域:域类别=等式和公式;域名=Eq;(创建科学公式);域选项(开关)如下表:

    开关

    说明

    \A()

    用任何编号的参数绘制二维矩阵

    \B()

    用适合元素大小的括弧来括住单个的元素

    \D()

    精确控制下一个字符的水平位置

    \F(,)

    创建在分数线上下居中的分数

    \I(,,)

    利用上限、下限和被积函数建立一个积分

    \L()

    创建数值列表

    \O()

    将每一个连续的元素重叠在其前一个元素上

    \R()

    绘制根式

    \S()

    将元素设置为上标或下标

    \X()

    在括号内文字的周围创建一个方框

    下面看几个比较实用的例子:

      “EQ \i(-1,1,3x+7)”表示“3x+7”从-1到1的积分,开关\i(,,)能使用指定的符号或默认符号及三个元素创建积分。

      “EQ \r(2,3m-2)”表示3m-2的平方根,(开关\r(,)能使用一个或两个元素绘制根号。

      “EQ \f(7,8)”表示八分之七 ,(开关\f(,)能创建分数)。

      “EQ \a \ac \co2 \vs3 \hs3(Ax,By,C,D)”表示一个二维数组。

    “EQ \b \bc\{ (\r(3,x+y))” ,表示用方括号括住单个元素。

    再看看复杂公式:

     

    OK. Let’s Use .Net Programming:

    有了Word域的基本知识后。如何用.NET实现,简单了,不过几行代码而已:

      

    /// <summary>

            
    /// 在Word中添加公式一、公式二

            
    /// </summary>

            
    /// <param name="sender"></param>

            
    /// <param name="e"></param>

       
    private void button1_Click(object sender, EventArgs e)

            {           

                
    object oEndOfDoc = "\\endofdoc";

                WordObject wd 
    = new WordObject();

                wd.CreateNewDocument(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) 
    + @"\ax.doc"true);

                Word.Range range 
    = wd.Document.Bookmarks.get_Item(ref oEndOfDoc).Range;

                
    object fieldType = Word.WdFieldType.wdFieldEmpty;

                
    object formula = @"EQ \R(3,a*\f(d,b\s(2))+\f(b+c+d,b-c-x)) ";//公式一

                
    object preserveFormatting = false;

                
    //字体设置

                range.Text 
    = formula.ToString();

                range.Font.Size 
    = 20;//字体大小

                range.Font.Bold 
    = 0;//粗体

                range.Font.Subscript 
    = 0;//是否为下标1为下标

                range.Font.Color 
    = Word.WdColor.wdColorYellow;//所选字体颜色            

                wd.Document.Fields.Add(range, 
    ref fieldType, ref formula, ref preserveFormatting);//插入第一个公式

     

                formula 
    = @"EQ U\S\do(x)=\R(,U\s\do(z)\s(2)-U\s\do(r)\s(2))(%) ";//公式二

                
    //字体设置

                range.Text 
    = formula.ToString();

                range.Font.Size 
    = 20;//字体大小

                range.Font.Bold 
    = 0;//粗体

                range.Font.Subscript 
    = 0;//是否为下标1为下标

                range.Font.Color 
    = Word.WdColor.wdColorRed;//所选字体颜色        

                Word.Field fd 
    = wd.Document.Fields.Add(range, ref fieldType, ref formula, ref preserveFormatting);//插入第二个公式

     

            }

     

    操作Word的帮助类

     


     #region WordObject
        
    public class WordObject
        
    {
            
    static object oMissing = System.Reflection.Missing.Value;
            
    static Object oFalse = false;
            
    static Object oTrue = true;
            
    private Word.Application WordApp;
            
    private Word.Document WordDoc;
            
    private string errorMsg;
            
    /// <summary>
            
    /// Word 应用程序对象。
            
    /// </summary>

            public Word.Application Application
            
    {
                
    get
                
    {
                    
    return WordApp;
                }

            }


            
    /// <summary>
            
    /// Word 文档对象。
            
    /// </summary>

            public Word.Document Document
            
    {
                
    get
                
    {
                    
    return WordDoc;
                }

            }

            
    /// <summary>
            
    /// 属性:错误信息
            
    /// </summary>

            public string ErrorMsg
            
    {
                
    get
                
    {
                    
    return errorMsg;
                }

                
    set
                
    {
                    errorMsg 
    = value;

                }


            }



            
    /// <summary>
            
    /// 构造函数,置空
            
    /// </summary>

            public WordObject()
            
    {
                WordApp 
    = null;
                WordDoc 
    = null;
            }




            
    #region 打开

            
    /// <summary>
            
    /// 取得Word应用程序对象。
            
    /// </summary>
            
    /// <returns></returns>

            private Word.Application GetApplication()
            
    {
                
    if (WordApp != null)
                
    {
                    
    try
                    
    {
                        WordApp.Dummy2();
                    }

                    
    catch
                    
    {


                    }

                }

                
    if (WordApp == null)
                
    {
                    WordApp 
    = new Word.Application();

                }

                
    return WordApp;

            }

            
    /// <summary>
            
    /// 打开已存在的Word文档。
            
    /// </summary>
            
    /// <param name="bVisible">是否打开Word窗口使得用户可以看到。</param>
            
    /// <returns></returns>

            public bool Open(string filename, bool bVisible)
            
    {

                GetApplication();

                
    try
                
    {

                    WordApp.Visible 
    = bVisible;

                    
    if (WordDoc != null)
                    
    {

                        WordDoc.Close(
    ref oFalse, ref oMissing, ref oMissing);

                        WordDoc 
    = null;

                    }


                    
    object oFileName = filename;

                    WordDoc 
    = WordApp.Documents.Open(

                    
    ref oFileName, ref oMissing, ref oMissing, ref oMissing,

                    
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                    
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                    
    ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                    WordDoc.Activate();

                }


                
    catch (Exception exp)
                
    {

                    ErrorMsg 
    = exp.Message + "rn" + exp.StackTrace;

                    
    return false;

                }




                ErrorMsg 
    = string.Empty;

                
    return true;

            }


            
    /// <summary>

            
    /// 根据模板,新建Word一个文档。

            
    /// </summary>

            
    /// <param name="filename">模板路径。模板为空,则新建空白文档。</param>

            
    /// <param name="bVisible">是否打开Word窗口。</param>

            
    /// <returns></returns>


            
    public bool CreateNewDocument(string tplFileName, bool bVisible)
            
    {

                GetApplication();

                WordApp.Visible 
    = bVisible;

                
    try
                
    {

                    
    if (WordDoc != null)
                    
    {

                        WordDoc.Close(
    ref oFalse, ref oMissing, ref oMissing);

                        WordDoc 
    = null;

                    }




                    
    object oVisible = bVisible;

                    
    object oFileName = tplFileName;

                    
    if (tplFileName != null && tplFileName.Length > 0)
                    
    {

                        WordDoc 
    = WordApp.Documents.Add(ref oFileName, ref oFalse, ref oMissing, ref oVisible);
                    }


                    
    else
                    
    {

                        WordDoc 
    = WordApp.Documents.Add(ref oMissing, ref oFalse, ref oMissing, ref oVisible);
                    }


                    WordDoc.Activate();

                }

                
    catch (Exception exp)
                
    {
                    
    try
                    
    {
                        
    object oVisible = bVisible;
                        WordDoc 
    = WordApp.Documents.Add(ref oMissing, ref oFalse, ref oMissing, ref oVisible);
                    }

                    
    catch (Exception excc)
                    
    {

                        ErrorMsg 
    = exp.Message + "rn" + exp.StackTrace + "rn" + excc.Message;
                        
    return false;
                    }



                }


                ErrorMsg 
    = string.Empty;

                
    return true;

            }




            
    #endregion

        }

        
    #endregion

    这是带有字体设置效果的:

     

    问题解决了。呵呵。以前还看到有专门卖这种第三方控件的。今天让他歇业。

  • 相关阅读:
    Java WebService入门实例
    Maven是什么
    for 循环
    2.4 DevOps工程师的故事:构建运行时的严谨性
    2.3.3 构建微服务的入口:Spring Boot控制器
    2.3.2 引导Spring Boot应用程序:编写引导类
    2.1.3 互相交流:定义服务接口
    第2章 使用Spring Boot构建微服务
    第1章 欢迎来到Cloud和Spring
    第一次在博客园开博
  • 原文地址:https://www.cnblogs.com/chuncn/p/1771596.html
Copyright © 2011-2022 走看看