gpt4 book ai didi

c# - Docx内容替换

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:00 26 4
gpt4 key购买 nike

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("@@username@@");
docText = regexText.Replace(docText, "john thomas ");

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

这应该用代码中的名称替换 doctext 上的匹配项。我检查了文档文本,要替换的词 (@@username@@) 被拆分了。之间有时会有 XML 内容@@用户名@@。有时单词本身的格式是错误的。

如何替换 @@username@@

最佳答案

一个简单的解决方法是将标签放在文本框或表格中(甚至可以使用预先指定的 ID)。我不确定文档中是否还有其他表格或文本框,但抓取表格中的内容或文档中唯一的文本框内的内容真的很容易。

关于c# - Docx内容替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com