openxml-sdk - 使用 settings.xml 创建 word 2007 文件

发布于 2024-11-08 13:48:27 字数 844 浏览 3 评论 0原文

我正在尝试使用以下代码生成一个新的Word文档。生成的 Word 文档没有 settings.xml。我需要在word文件中有settings.xml。任何帮助将不胜感激。

public static byte[] GenerateNewDocument()
{
    byte[] returnValue = null;
    MemoryStream stream = null;
    WordprocessingDocument wordDoc = null;

    try
    {
        stream = new System.IO.MemoryStream();
        wordDoc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);
    }
    catch
    {
        if (stream != null)
        {
            stream.Close();
        }

        throw;
    }

    using (wordDoc)
    {
        wordDoc.AddMainDocumentPart();
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        mainPart.Document = new Document(new Body());             
        mainPart.Document.Save();
    }

    returnValue = stream.ToArray();
    return returnValue;
}

I am trying to generate a new Word document using the following code. The Word document gets generated without settings.xml. I need to have settings.xml in the word file. Any help would be appreciated.

public static byte[] GenerateNewDocument()
{
    byte[] returnValue = null;
    MemoryStream stream = null;
    WordprocessingDocument wordDoc = null;

    try
    {
        stream = new System.IO.MemoryStream();
        wordDoc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);
    }
    catch
    {
        if (stream != null)
        {
            stream.Close();
        }

        throw;
    }

    using (wordDoc)
    {
        wordDoc.AddMainDocumentPart();
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        mainPart.Document = new Document(new Body());             
        mainPart.Document.Save();
    }

    returnValue = stream.ToArray();
    return returnValue;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-11-15 13:48:27

您需要创建自己的DocumentSettingsPart,然后将其插入到MainDocumentPart中。因此,设置部分可能如下所示:

<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vm" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
 <w:defaultTabStop w:val="475"/>
 <w:compat>
 <w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="14"/>
 </w:compat>
</w:settings>

然后将其保存为“settings.xml”,您可以使用如下代码:

private static void AddSettingsToMainDocumentPart(MainDocumentPart part)
    {
      DocumentSettingsPart settingsPart = part.AddNewPart<DocumentSettingsPart>();
      FileStream settingsTemplate = new FileStream("settings.xml", FileMode.Open, FileAccess.Read);
      settingsPart.FeedData(settingsTemplate);
      settingsPart.Settings.Save();
    }

You need to create your own DocumentSettingsPart and then insert it into the MainDocumentPart. So the settings part may look like this:

<w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vm" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main">
 <w:defaultTabStop w:val="475"/>
 <w:compat>
 <w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="14"/>
 </w:compat>
</w:settings>

And then having that saved somewhere as "settings.xml", you could use code like this:

private static void AddSettingsToMainDocumentPart(MainDocumentPart part)
    {
      DocumentSettingsPart settingsPart = part.AddNewPart<DocumentSettingsPart>();
      FileStream settingsTemplate = new FileStream("settings.xml", FileMode.Open, FileAccess.Read);
      settingsPart.FeedData(settingsTemplate);
      settingsPart.Settings.Save();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文