C# 保存从模板生成的 OpenXML 文档 (Word)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1233228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving an OpenXML Document (Word) generated from a template
提问by DilbertDave
I have a bit of code that will open a Word 2007 (docx) document and update the appropriate CustomXmlPart (thus updating the Content Controls in the document itself as they are mapped to the CustomXmlPart) but can't work out how to save this as a new file.! Surely it can't be that hard!
我有一些代码可以打开 Word 2007 (docx) 文档并更新相应的 CustomXmlPart(从而更新文档本身中的内容控件,因为它们被映射到 CustomXmlPart),但无法弄清楚如何将其另存为一个新文件。!当然不可能那么难!
My current thinking is that I need to open the template and copy the content into a new, blank document - file by file, updating the CustomXmlPart when I encounter it. Call me old fashioned but that sounds a little bit clunky to me!
我目前的想法是我需要打开模板并将内容复制到一个新的空白文档中 - 一个文件一个文件,当我遇到它时更新 CustomXmlPart。称我为老式,但这对我来说听起来有点笨拙!
Why can't I just do a WordprocessingDocument.SaveAs(filename); ...?
为什么我不能只做一个 WordprocessingDocument.SaveAs(filename); ……?
Please tell me I am missing something simple here.
请告诉我我在这里遗漏了一些简单的东西。
Thanks in advance
提前致谢
采纳答案by Ahmad Mageed
Are you referring to the OpenXml SDK? Unfortunately, as of OpenXml SDK 2.0, there's no SaveAs method. You'll need to:
你指的是 OpenXml SDK 吗?不幸的是,从 OpenXml SDK 2.0 开始,没有 SaveAs 方法。你需要:
- Make a temporary copy of your template file, naming it whatever you want.
- Perform your OpenXml changes on the above file.
- Save the appropriate sections (ie. using the .
myWordDocument.MainDocumentPart.Document.Save()
method for the main content orsomeHeaderPart.Header.Save()
method for a particular header).
- 制作模板文件的临时副本,随意命名。
- 对上述文件执行 OpenXml 更改。
- 保存适当的部分(即使用 .
myWordDocument.MainDocumentPart.Document.Save()
方法用于主要内容或someHeaderPart.Header.Save()
用于特定标题的方法)。
回答by Héctor Espí Hernández
Indeed you can, at least, in OpenXml SDK 2.5. However, watchout to work with a copy of the original file, because changes in the XML will be actually reflected in the file. Here you have the methods Load and Save of my custom class (after removing some validation code,...):
事实上,您至少可以在 OpenXml SDK 2.5 中使用。但是,请注意使用原始文件的副本,因为 XML 中的更改实际上会反映在文件中。这里有我的自定义类的加载和保存方法(删除一些验证代码后,...):
public void Load(string pathToDocx)
{
_tempFilePath = CloneFileInTemp(pathToDocx);
_document = WordprocessingDocument.Open(_tempFilePath, true);
_documentElement = _document.MainDocumentPart.Document;
}
public void Save(string pathToDocx)
{
using(FileStream fileStream = new FileStream(pathToDocx, FileMode.Create))
{
_document.MainDocumentPart.Document.Save(fileStream);
}
}
Having "_document" as a WordprocessingDocumentinstance.
将“_document”作为WordprocessingDocument实例。
回答by user3285954
In Open XML SDK 2.5 Close saves changes when AutoSave is true. See my answer here: https://stackoverflow.com/a/36335092/3285954
在 Open XML SDK 2.5 中,当 AutoSave 为真时关闭保存更改。在此处查看我的答案:https: //stackoverflow.com/a/36335092/3285954
回答by Boris Lipschitz
You can use a MemoryStream to write the changes, rather than in the original file. Consequently, you can save that MemoryStream to a new file:
您可以使用 MemoryStream 来写入更改,而不是在原始文件中。因此,您可以将该 MemoryStream 保存到一个新文件中:
byte[] byteArray = File.ReadAllBytes("c:\temp\mytemplate.docx");
using (var stream = new MemoryStream())
{
stream.Write(byteArray, 0, byteArray.Length);
using (var wordDoc = WordprocessingDocument.Open(stream, true))
{
// Do work here
// ...
wordDoc.MainDocumentPart.Document.Save(); // won't update the original file
}
// Save the file with the new name
stream.Position = 0;
File.WriteAllBytes("C:\temp\newFile.docx", stream.ToArray());
}