如何使用 OpenXML 构建自定义句子
我想知道是否有一种方法可以“玩”.docx 中的句子。
这就是我需要做的:
我的文档中有一个段落
示例: 这是我需要根据某些条件格式化的段落,但我不知道如何使用 openxml sdk 来做到这一点。 示例结束。
因此,根据我在 c# 中评估的条件,我想添加/删除文本。您应该知道的另一件事是,我希望产品所有者更改文档的文本。
基本上,我想要实现的是拥有一个产品所有者可以随意编辑的模板文档,但在该文档中,文本可能会根据文档的生成对象而发生变化。
谢谢
I would like to know if there is a way to "play" with sentences in a .docx.
Here's what I need to do:
I have a paragraph in a document
exemple of my paragraph:
This is a paragraph that I need to format based on some conditions and I can't figure how to do this with openxml sdk.
end of exemple.
So based on a condition that I evaluate in c# I would like to add/remove the text. Another thing that you should know id that I would like the product owner to change the text of the document.
Basicly what I want to acheive is having a template document that my product owner can edit at will but in this document the text might change based on for whom the document is produce.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 Word 中创建模板文档,并在要动态插入文本的每个位置创建内容控件。对于每个创建的内容控件,您应该设置唯一的Tag属性值。
在您的 C# 应用程序代码中,您可以很容易地通过其标签找到内容控件。找到它后,您可以保存其父节点,删除内容控件并插入所需的文本作为先前保存的父节点的子段落元素。
您可以使用“开发人员”选项卡添加内容控件。如果您看不到它,可以使用此处描述的步骤来显示它:http://msdn.microsoft.com/en-us/library/bb608625.aspx。
You can create a template document in Word and create a content control in each place you want the text to be dynamically inserted. For each created content control, you should set unique Tag property value.
In your c# application code, you can then find a content control by its tag quite easily. After you find it, you can save its parent node, remove the content control and insert the text you want as a child paragraph element for the parent node saved earlier.
You can add content controls by using Developer tab. If you can't see it, you can use steps described here in order to show it: http://msdn.microsoft.com/en-us/library/bb608625.aspx .
替换文本涉及一些复杂性,因为文本可以分为多个运行。如果您希望您的所有者能够编辑文档,这是您必须解决的问题。幸运的是,您可以将示例代码用作 PowerTools for Open XML 的一部分。 PowerTools 是作为 PowerShell 的 cmdlet 编写的,但您可以将核心 C# 代码用于您自己的程序。 TextReplacer.cs 模块应该为您提供一个良好的起点。您将需要它所依赖的一些其他模块,例如 PtOpenXmlDocument.cs、PtOpenXmlUtil.cs 和 PtUtil.cs。我希望这有帮助。
There are some complexities involved in replacing text because text can be broken into multiple runs. If you want your owner to be able to edit the document, this is a problem you must solve. Luckily, there is sample code that you can use as part of the PowerTools for Open XML. PowerTools were written as cmdlets for PowerShell, but you can take the core C# code for your own programs. The TextReplacer.cs module should give you a good starting point. You will need some of the other modules that it depends on, like PtOpenXmlDocument.cs, PtOpenXmlUtil.cs and PtUtil.cs. I hope that helps.