【发布时间】:2012-02-07 17:50:24
【问题描述】:
我正在尝试打开一个 Word 文档,更改一些文本,然后将更改保存到一个新文档中。我可以使用下面的代码完成第一步,但我不知道如何将更改保存到新文档(指定路径和文件名)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
namespace WordTest
{
class Program
{
static void Main(string[] args)
{
string template = @"c:\data\hello.docx";
string documentText;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(template, true))
{
using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
documentText = reader.ReadToEnd();
}
documentText = documentText.Replace("##Name##", "Paul");
documentText = documentText.Replace("##Make##", "Samsung");
using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
writer.Write(documentText);
}
}
}
}
}
我是这方面的初学者,所以请原谅这个基本问题!
【问题讨论】:
-
对于那些通过谷歌得到这个并且看到我们基本上只是遵循这个的人:docs.microsoft.com/en-us/office/open-xml/… 除了我们试图将它保存在一个新文档中,而不是原始文档中。
标签: c# openxml openxml-sdk office-2007