打开XML SDK:打开Word模板并保存到其他文件名 [英] Open XML SDK: opening a Word template and saving to a different file-name
问题描述
这是一件很简单的事情,我找不到正确的技术.我想要的是打开一个.dotx模板,进行一些更改并保存为.docx扩展名相同的名称.我可以保存一个WordprocessingDocument,但只能保存到它的加载位置.我尝试使用WordprocessingDocument手动构建一个新文档,但进行了更改,但到目前为止没有任何效果,我尝试了MainDocumentPart.Document.WriteTo(XmlWriter.Create(targetPath));
,只是得到了一个空文件.
This one very simple thing I can't find the right technique. What I want is to open a .dotx template, make some changes and save as the same name but .docx extension. I can save a WordprocessingDocument but only to the place it's loaded from. I've tried manually constructing a new document using the WordprocessingDocument with changes made but nothing's worked so far, I tried MainDocumentPart.Document.WriteTo(XmlWriter.Create(targetPath));
and just got an empty file.
这里正确的方法是什么?就SDK而言,.dotx文件是完全特殊还是仅是另一个文档-我是否应该简单地将模板复制到目标位置,然后打开 that 并进行更改并保存?如果一次从两个客户端调用我的应用程序,是否可以两次打开同一个.dotx文件,我确实有些担心……在这种情况下,创建一个副本还是很明智的……但是出于我自己的好奇心,我仍然想要知道如何执行另存为".
What's the right way here? Is a .dotx file special at all or just another document as far as the SDK is concerned - should i simply copy the template to the destination and then open that and make changes, and save? I did have some concerns if my app is called from two clients at once, if it can open the same .dotx file twice... in this case creating a copy would be sensible anyway... but for my own curiosity I still want to know how to do "Save As".
推荐答案
我建议仅使用File.IO将dotx文件复制到docx文件,然后在其中进行更改(如果适合您的情况).您还必须调用一个ChangeDocumentType函数,以防止新docx文件中出现错误.
I would suggest just using File.IO to copy the dotx file to a docx file and make your changes there, if that works for your situation. There's also a ChangeDocumentType function you'll have to call to prevent an error in the new docx file.
File.Copy(@"\path\to\template.dotx", @"\path\to\template.docx");
using(WordprocessingDocument newdoc = WordprocessingDocument.Open(@"\path\to\template.docx", true))
{
newdoc.ChangeDocumentType(WordprocessingDocumentType.Document);
//manipulate document....
}
这篇关于打开XML SDK:打开Word模板并保存到其他文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!