简体   繁体   English

使用正则表达式C#替换文档中的文本字段

[英]Replacing a text field in a document using regular expression c#

I got a simple c# code that can Replace Fields like First name, Middle Name, Last Name in a word document I used Microsoft office object library but I just now found out that Windows word is not allowed in the system so I need to read the Doc File and use a regular expression to Replace the Fields 我有一个简单的C#代码,可以在Word文档中替换字段(如名字,中间名,姓氏),但我只是使用Microsoft Office对象库,但现在我发现系统中不允许使用Windows单词,因此我需要阅读Doc File并使用正则表达式替换字段

Ex: I [FirstName] [LastName], Son of [Fathers Name] & [Mothers Name] From [city]. 例如:我[名字] [姓氏],[父亲姓名]和[母亲姓名]的儿子,来自[城市]。 Address [First Name],[Address Line 1] 地址[名字],[地址行1]

Now there are going to be eight such templates and need to replace the details FirstName(ex: John) in place of [First Name] LastName(ex: Doe) in place of [LastName] if the middle name is not given then remove the [MiddleName] and create new doc file. 现在将有八个这样的模板,并且如果没有给出中间名,则需要替换详细信息FirstName(ex:John)代替[First Name] LastName(ex:Doe)代替[LastName],然后删除[MiddleName]并创建新的doc文件。 These fields may be repeated n number of times can someone please give me an example of how to do this or is regular expression necessary for this(at least 20 such fields in each template ) if I use Find and Replace it is too much of unnecessary code Am I right? 这些字段可能会重复n次,有人可以给我举一个例子,或者如果我使用Find and Replace这样做是正则表达式所必需的(每个模板中至少有20个这样的字段),那么这是不必要的代码我说的对吗?

Thank You in Advance 先感谢您

You might want to take a look to the Open XML SDK . 您可能需要看一下Open XML SDK You can directly access to the underlying XML of the docx and you don't need Office installed. 您可以直接访问docx的基础XML,而无需安装Office。

This example might also help you. 这个例子也可能对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM