通过阅读DOCX模板创建新的DOCX文件(其内容已被替换)

时间:2010-04-09 03:35:50

标签: file new-operator docx

到目前为止,代码读取模板并替换为新值,最后用新值替换docx文件。任何人都可以告诉我如何以不同的名称保存替换的docx文件吗?。

我的代码如下:

   using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }

            Regex regexText = new Regex("#ApplicationCompleteDate#");
            docText = regexText.Replace(docText,DataHolding.ApplicationCompleteDate);

            regexText = new Regex("#ApplicantPrivateAddress#");
            docText = regexText.Replace    (docText,UserDataHolding.ApplicantPrivateAddress);


       using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream   (FileMode.Create)))
            {
                sw.Write(docText);
            }
        }

如果有人通过更改上面的代码来帮助我创建新的docx文件,那对我来说非常有帮助。

1 个答案:

答案 0 :(得分:0)

Dim sw As StreamWriter = New StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))
    sw.Write(docText)
    sw.Close()
    sw.Dispose()