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

发布于 2024-08-28 05:02:09 字数 955 浏览 8 评论 0原文

到目前为止,代码读取模板并替换为新值,最后用新值替换 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 文件,这对我来说将非常有帮助。

Up to now code is read the template and replace with new values and finally replace the docx file with new values. Can any one please tell me how to save the replaced docx file in diffrent name?.

My code is bellow.

   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);
            }
        }

If any one help me with this creating new docx file by changing above code, it will be very helpful for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情换悲伤 2024-09-04 05:02:09
Dim sw As StreamWriter = New StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))
    sw.Write(docText)
    sw.Close()
    sw.Dispose()
Dim sw As StreamWriter = New StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))
    sw.Write(docText)
    sw.Close()
    sw.Dispose()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文