使用Open Office SDK 2.0将合并字段添加到MS Word文档

时间:2016-04-18 17:51:15

标签: c# ms-word openxml-sdk

我正在尝试使用Microsoft Open Office SDK(v2.0)创建新的MS Word文档并插入新的合并字段,但下面的代码不起作用(当我保存结果时,合并字段不会出现流到文件并打开它)。我在互联网上搜索过但无法解决问题。

            MemoryStream stream = new MemoryStream();

        using (
            WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream,
                DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
        {
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

            mainPart.Document = new Document();
            Body body = mainPart.Document.AppendChild(new Body());

            FieldCode fieldCode = new FieldCode(" MERGEFIELD MyMergeField ");
            body.AppendChild(fieldCode);
            mainPart.Document.Save();
        }

        return stream;

感谢您的帮助。

0 个答案:

没有答案