使用OpenXML将文本替换为MS Word中的等式

时间:2017-04-15 14:12:09

标签: c# .net openxml openxml-sdk

您好我需要将模板中的文本替换为数学方程式。我想通过替换段落或他的部分来做到这一点。我正在尝试使用OpenXML。我的程序在开始时打开一个模板并复制到新的word文档,然后我在查找每个段落的特定单词,当我发现它将整个段落替换为新的等式。程序将编译并完成工作,但是当我尝试打开新文档时,我不能因为出现错误:内容出现问题。

在文件ExampleXML.xml中,我有xml我要替换的内容:

<?xml verios="1.0"?>
<w:p w:rsidRPr="00A750D6" w:rsidR="00212F73" w:rsidP="005437B7" w:rsidRDefault="0027760B" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<m:oMath>
<m:r>
    <w:rPr>
        <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
    </w:rPr>
        <m:t>a=</m:t>
</m:r>
<m:f>
    <m:fPr>
        <m:ctrlPr>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
                <w:i />
            </w:rPr>
        </m:ctrlPr>
    </m:fPr>
    <m:num>
        <m:r>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
            </w:rPr>
            <m:t>c</m:t>
        </m:r>
    </m:num>
    <m:den>
        <m:r>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
            </w:rPr>
            <m:t>b</m:t>
        </m:r>
    </m:den>
</m:f>
<m:r>
    <w:rPr>
        <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
    </w:rPr>
    <m:t>*</m:t>
</m:r>
<m:rad>
    <m:radPr>
        <m:degHide m:val="1" />
        <m:ctrlPr>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
                <w:i />
            </w:rPr>
        </m:ctrlPr>
    </m:radPr>
    <m:deg />
    <m:e>
        <m:r>
            <w:rPr>
                <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
            </w:rPr>
            <m:t>a</m:t>
        </m:r>
    </m:e>
</m:rad>
</m:oMath>
</m:oMathPara>
<w:bookmarkStart w:name="_GoBack" w:id="0" />
<w:bookmarkEnd w:id="0" /></w:p>

这是我的代码:

public void CreateXmlDocument()         {

        var wzor = File.ReadAllText(@"E:\ExampleXML.xml");
        using (var template = File.Open(@"E:\ExampleDOCX.docx", FileMode.Open, FileAccess.Read))
        {
            using (MemoryStream stream = new MemoryStream())
            {
                template.CopyTo(stream);

                using (WordprocessingDocument WordTemplate = WordprocessingDocument.Open(stream, true))
                {

                    MainDocumentPart mainDocumentPart = WordTemplate.MainDocumentPart;

                    foreach (var paragraph in mainDocumentPart.Document.Descendants<Paragraph>().ToList())
                    {
                        var parent = paragraph.Parent;

                        var paragraph_xml = (paragraph.OuterXml);
                        bool containsParam = false;

                        if (paragraph.InnerText.Contains("test"))
                            {
                                var tekst_replace = paragraph_xml;

                                paragraph_xml = paragraph_xml.Replace(tekst_replace, wzor);
                                containsParam = true;
                            }
                        if (containsParam)
                        {
                            parent.InsertBefore(new Paragraph(paragraph_xml), paragraph);
                            paragraph.Remove();
                        }
                    }
                    mainDocumentPart.Document.Save();
                    stream.Seek(0, SeekOrigin.Begin);
                }

                using (var fileStream = File.Create(@"E:\po.docx"))
                {
                    stream.CopyTo(fileStream);
                }
            }
        }
    }

有什么问题?或者我怎样才能将文字改为等式? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

来自你的代码:

var wzor = File.ReadAllText(@"E:\ExampleXML.xml");

...

var paragraph_xml = (paragraph.OuterXml);

...

var tekst_replace = paragraph_xml;

paragraph_xml = paragraph_xml.Replace(tekst_replace, wzor);

看起来这可以简化为:

string a = "a";
string b_0 = "b";
string b_1 = b_0; // b_1 == "b"

b_0 = b_1.Replace(b_0, a); // "b".Replace("b", "a")

那么为什么不直接设置b_0 = a

或者在您的情况下,替换:

var paragraph_xml = (paragraph.OuterXml);
bool containsParam = false;

if (paragraph.InnerText.Contains("test"))
{
    var tekst_replace = paragraph_xml;

    paragraph_xml = paragraph_xml.Replace(tekst_replace, wzor);
    containsParam = true;
}
if (containsParam)
{
    parent.InsertBefore(new Paragraph(paragraph_xml), paragraph);
    paragraph.Remove();
}

使用:

if (paragraph.InnerText.Contains("test"))
{
    parent.InsertBefore(new Paragraph(wzor), paragraph);
    paragraph.Remove();
}