Open Xml Sdk创建公式
距离上一篇openxml的文章已经过去很久了,这段时间一直忙于openxml来自动生成word。把自动生成word的心得写出来,这一篇就写插入word2007数学公式吧。
在写插入数学公式之前先回顾一下openxml的相关知识,openxml是基于xml和zip协议的文档描述格式。所以我们可以将word的中一个个组成看成一个个标签对,自动生成word的内容就是在生成一个个标签对,然后插入内容,插入到文档标签中。使用openxml生成word速度比微软自带的接口速度快了十几倍,不过稍显不足的是word2003不是openxml格式的。
Paragraph是openxml中的段落标签,这个标签是word的基本组成部分,word中除了表格标签外,其他的内容都是放在Paragraph中的。
OfficeMath即数学公式标签。
下面是示例代码,这里先只写一个简单的公式,有空我在把常用的公式整理出来
代码
公式类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using W = DocumentFormat.OpenXml.Wordprocessing; using M = DocumentFormat.OpenXml.Math; using DocumentFormat.OpenXml; namespace OMath { /// <summary> /// 生成公式的类 /// 这段代码是使用word2007插入新公式后,使用Open XML SDK 2.0 Productivity Tool反编译出c#代码写出来的 /// </summary> class MathHelper { #region 常用变量 public static string fontName = "微软雅黑"; //四号字,对应word中的大小是10.5(Ps:这是openxml的一个不好之处就是,单位和word中显示的不一致) public static string fontSize = "21"; public static string fontSizeComplexScript = "24"; #endregion #region 插入文档的公式 public static M.OfficeMath GetSubScript(string baseString, string downString) { M.OfficeMath officeMath = new M.OfficeMath(); M.Subscript subScript = GenerateSubscript(baseString, downString); officeMath.Append(subScript); return officeMath; } public static M.OfficeMath GetSubScript(string baseString, string downString, string runText) { M.OfficeMath officeMath = new M.OfficeMath(); M.Subscript subScript = GenerateSubscript(baseString, downString); M.Run run = GenerateMathRun(runText); officeMath.Append(subScript); officeMath.Append(run); return officeMath; } #endregion #region 基础公式 /// <summary> /// 带有下标的公式 /// </summary> /// <param name="baseString">基础字符</param> /// <param name="downString">下标字符</param> /// <returns></returns> public static M.Subscript GenerateSubscript(string baseString, string downString) { M.Subscript subscript1 = new M.Subscript(); M.Base base1 = new M.Base(); M.Run run1 = GenerateMathRun(baseString); base1.Append(run1); M.SubArgument subArgument = new M.SubArgument(); M.Run run = GenerateMathRun(downString); subArgument.Append(run); subscript1.Append(base1); subscript1.Append(subArgument); return subscript1; } /// <summary> /// 带有下标的公式 /// </summary> /// <param name="baseElement">基础元素</param> /// <param name="downElement">下标元素</param> /// <returns></returns> public static M.Subscript GenerateSubscript(OpenXmlElement baseElement, OpenXmlElement downElement) { M.Subscript subscript1 = new M.Subscript(); M.Base base1 = new M.Base(); base1.Append(baseElement); M.SubArgument subArgument = new M.SubArgument(); subArgument.Append(downElement); subscript1.Append(base1); subscript1.Append(subArgument); return subscript1; } /// <summary> /// 带有上标的公式 /// </summary> /// <param name="baseString">基础字符</param> /// <param name="upString">上标字符</param> /// <returns></returns> public static M.Superscript GenerateSuperscript(string baseString, string upString) { M.Superscript superscript = new M.Superscript(); M.Base base1 = new M.Base(); M.Run run1 = GenerateMathRun(baseString); base1.Append(run1); M.SuperArgument superArgument = new M.SuperArgument(); M.Run run = GenerateMathRun(upString); superArgument.Append(run); superscript.Append(base1); superscript.Append(superArgument); return superscript; } /// <summary> /// 带有上标的公式 /// </summary> /// <param name="baseElement">基础元素</param> /// <param name="upElement">上标元素</param> /// <returns></returns> public static M.Superscript GenerateSuperscript(OpenXmlElement baseElement, OpenXmlElement upElement) { M.Superscript superscript = new M.Superscript(); M.Base base1 = new M.Base(); base1.Append(baseElement); M.SubArgument subArgument = new M.SubArgument(); subArgument.Append(upElement); superscript.Append(base1); superscript.Append(subArgument); return superscript; } #endregion #region run,是存放最基础的文本信息的标签 private static M.Run GenerateMathRun(string baseString) { M.Run run1 = new M.Run(); W.RunProperties runProperties = new W.RunProperties(); //字体 W.RunFonts runFonts1 = new W.RunFonts() { Ascii = fontName, HighAnsi = fontName, EastAsia = fontName }; //字的大小 W.FontSize fontSize1 = new W.FontSize() { Val = fontSize }; W.FontSizeComplexScript fsc = new W.FontSizeComplexScript { Val = fontSizeComplexScript }; run1.Append(new W.RunProperties(runFonts1, fontSize1, fsc)); run1.Append(new W.Text() { Text = baseString }); return run1; } #endregion } }
word处理类:
using W = DocumentFormat.OpenXml.Wordprocessing; using M = DocumentFormat.OpenXml.Math; using System; using System.Collections.Generic; using System.Linq; using System.Text; using DocumentFormat.OpenXml.Packaging; using System.IO; using DocumentFormat.OpenXml; namespace OMath { public class OpenWord { public WordprocessingDocument wDoc; public MainDocumentPart mainDoc; public W.Body body; public W.Document doc; public static string fontName = "微软雅黑"; //四号字,对应word中的大小是10.5(Ps:这是openxml的一个不好之处就是,单位和word中显示的不一致) public static string fontSize = "21"; /// <summary> /// 初始化文档 /// </summary> /// <param name="fileName">创建文档的路径名称</param> public void Init(string fileName) { if (File.Exists(fileName)) { File.Delete(fileName); } wDoc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document); mainDoc = wDoc.AddMainDocumentPart(); doc = new W.Document(); body = new W.Body(); } public void AddMath(M.OfficeMath oMath) { W.Paragraph paragraph = new W.Paragraph(); //修饰段落的属性, W.ParagraphProperties paragraphProperties1 = new W.ParagraphProperties(); //修饰段落中一些 W.ParagraphMarkRunProperties paragraphMarkRunProperties = new W.ParagraphMarkRunProperties(); //字体 W.RunFonts runFonts1 = new W.RunFonts() { Ascii = fontName, HighAnsi = fontName, EastAsia = fontName }; //字的大小 W.FontSize fontSize1 = new W.FontSize() { Val = fontSize }; paragraphMarkRunProperties.Append(runFonts1); paragraphMarkRunProperties.Append(fontSize1); //段落格式,这里设置段落居中(ps:在很多很多的word中或者是书籍中,公式一般都是居中滴) W.Justification justification = new W.Justification() { Val = W.JustificationValues.Center }; paragraphProperties1.Append(paragraphMarkRunProperties); paragraphProperties1.Append(justification); paragraph.Append(paragraphProperties1); //添加公式 paragraph.Append(oMath); //将短路添加入文档 body.Append(paragraph); } public void save() { doc.Body = body; mainDoc.Document = doc; wDoc.Close(); } } }
测试类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace OMath { class Program { static void Main(string[] args) { string FileName = "D:\\testMath.docx"; #region 创建word文档 OpenWord ow = new OpenWord(); ow.Init(FileName); ow.AddMath(MathHelper.GetSubScript("X","i")); ow.AddMath(MathHelper.GetSubScript("X", "i","=y+2")); ow.save(); #endregion } } }
效果图
相应的源码:https://files.cnblogs.com/weu135/OMath%E7%A4%BA%E4%BE%8B%E7%89%88.rar