openxml-sdk - 使用 settings.xml 创建 word 2007 文件
我正在尝试使用以下代码生成一个新的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建自己的
DocumentSettingsPart
,然后将其插入到MainDocumentPart
中。因此,设置部分可能如下所示:然后将其保存为“settings.xml”,您可以使用如下代码:
You need to create your own
DocumentSettingsPart
and then insert it into theMainDocumentPart
. So the settings part may look like this:And then having that saved somewhere as "settings.xml", you could use code like this: