如何找到&使用c#.net批量替换word文档中的文本? [英] How to find & replace text in word document in bulk using c#.net?
本文介绍了如何找到&使用c#.net批量替换word文档中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过使用c#.net批量查找和替换文本来提高程序的速度,我目前正在通过查找并逐个替换每个单词进行查找和替换。我想要批量更换
,以便提高速度和性能。该怎么办?,任何帮助都会很棒。
Hi,
I want to increase the speed of my program by find & replace text in bulk using c#.net, i am currently doing find & replace by finding & replacing each word one by one . i want to replace
it in bulk, so that speed & performance will increase. how to do it?, Any help would be great.
推荐答案
如果你想一次更换所有单词
step1:将所有单词存储在列表中
List< string> wordsToReplace = new List< string>(){age:-21,salary:-20000,Name:-vikas};
step2:创建表达式
string replacementString = String.Join(|,wordsToReplace);
step3:商店文件
string fileContent = System.IO.File.ReadAllText(Server.MapPath(〜/ TestFile.doc) ));
step4:执行替换
fileContent = Regex.Replace(fileContent,replacementString,animal)
step5:编写contenet
System.IO.File.WriteAllText(Server.MapPath(〜/ TestFile.doc) ),fileContent);
if you want replace all word at a time
step1: store all word in a list
List<string> wordsToReplace = new List<string>() { "age:-21","salary:-20000","Name:-vikas" };
step2: create an expression
string replacementString = String.Join("|",wordsToReplace);
step3: store document
string fileContent = System.IO.File.ReadAllText(Server.MapPath("~/TestFile.doc"));
step4: Perform replacement
fileContent = Regex.Replace(fileContent,replacementString, "animal")
step5:write contenet
System.IO.File.WriteAllText(Server.MapPath("~/TestFile.doc"), fileContent);
这篇关于如何找到&使用c#.net批量替换word文档中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文