首页 新闻 搜索 专区 学院

如何在word中写入公式?

0
悬赏园豆:100 [待解决问题]

要用程序把分式\指数等数学公式写入word,有开源的代码吗?
备注:word中的公式有多种,一种是"MathType Equation",一种是word2007后自带的公式.
我想要的最好是word2007后自带的公式.

浅蓝的主页 浅蓝 | 初学一级 | 园豆:54
提问于:2013-01-17 16:41
< >
分享
所有回答(2)
0

你打开Word,然后开启录制宏,然后用手操作在Word中插入数学公式,然后停止录制后通过VBA编辑器查看录制的宏的代码,然后你再用VSTO中的组件来实现就行了。

Launcher | 园豆:45045 (高人七级) | 2013-01-17 16:52

不引用word组件,直接用c#写入可以吗?没代码,提供个思路也好啊!

支持(0) 反对(0) 浅蓝 | 园豆:54 (初学一级) | 2013-01-18 09:09

@浅蓝: Open XML SDK(不支持2003).

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2013-01-18 09:19
1

如果不引用Word组件来插入公式,只能使用Open XML SDK来进行操作了,你可以在这里下载 Open XML(不过Open XML SDK操作不支持doc扩展名文件) http://www.microsoft.com/en-us/download/details.aspx?id=5124 

你可以在Word中手动插入公式后保存,用Open XML SDK 2.0 Productivity Tool打开Word文件,这个工具可以反射出代码,这样你可以仿照反射出来的代码来实现用Open XML SDK中的类来插入公式,下面是插入一个分式(例如 π(pai)/2          

)的代码:

View Code
 // Creates an Document instance and adds its children.
        public Document GenerateDocument()
        {
            Document document1 = new Document(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 wp14" }  };
            document1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
            document1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            document1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            document1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
            document1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
            document1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
            document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
            document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

            Body body1 = new Body();

            Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "009C73AA", RsidRunAdditionDefault = "00773B31" };

            M.Paragraph paragraph2 = new M.Paragraph();

            M.OfficeMath officeMath1 = new M.OfficeMath();

            M.Fraction fraction1 = new M.Fraction();

            M.FractionProperties fractionProperties1 = new M.FractionProperties();

            M.ControlProperties controlProperties1 = new M.ControlProperties();

            RunProperties runProperties1 = new RunProperties();
            RunFonts runFonts1 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };
            Italic italic1 = new Italic();

            runProperties1.Append(runFonts1);
            runProperties1.Append(italic1);

            controlProperties1.Append(runProperties1);

            fractionProperties1.Append(controlProperties1);

            M.Numerator numerator1 = new M.Numerator();

            M.Run run1 = new M.Run();

            RunProperties runProperties2 = new RunProperties();
            RunFonts runFonts2 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };

            runProperties2.Append(runFonts2);
            M.Text text1 = new M.Text();
            text1.Text = "π";

            run1.Append(runProperties2);
            run1.Append(text1);

            numerator1.Append(run1);

            M.Denominator denominator1 = new M.Denominator();

            M.Run run2 = new M.Run();

            RunProperties runProperties3 = new RunProperties();
            RunFonts runFonts3 = new RunFonts(){ Ascii = "Cambria Math", HighAnsi = "Cambria Math" };

            runProperties3.Append(runFonts3);
            M.Text text2 = new M.Text();
            text2.Text = "2";

            run2.Append(runProperties3);
            run2.Append(text2);

            denominator1.Append(run2);

            fraction1.Append(fractionProperties1);
            fraction1.Append(numerator1);
            fraction1.Append(denominator1);

            officeMath1.Append(fraction1);

            paragraph2.Append(officeMath1);
            BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
            BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };

            paragraph1.Append(paragraph2);
            paragraph1.Append(bookmarkStart1);
            paragraph1.Append(bookmarkEnd1);

            SectionProperties sectionProperties1 = new SectionProperties(){ RsidR = "009C73AA" };
            PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
            PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
            Columns columns1 = new Columns(){ Space = "720" };
            DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };

            sectionProperties1.Append(pageSize1);
            sectionProperties1.Append(pageMargin1);
            sectionProperties1.Append(columns1);
            sectionProperties1.Append(docGrid1);

            body1.Append(paragraph1);
            body1.Append(sectionProperties1);

            document1.Append(body1);
            return document1;
        }

你可以根据自己需要修改上面代码,上面代码也是用 Open XML SDK 2.0 Productivity Tool 反射出来的。

 

Learning hard | 园豆:362 (菜鸟二级) | 2013-01-22 17:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册