以编程方式编辑 Microsoft Word 文档
我想知道这是否可以做到。
我正在为我们的软件系统(学校项目)构建一个数据字典,并且我正在考虑一种自动化的方法来做到这一点。基本上我不怎么使用 Microsoft Word (2007),我只用它来记录学校的资料等。我想知道是否可以通过模板以编程方式创建/编辑 Word 文档。
我的想法是,我将在 Word 上创建一个页面,其中包含一个空表单,该表单将在每个页面上重复。对于我将输入到程序中的每个数据,它将更新表单中的相应字段并跳到下一个表单。
这样做的目的是消除复制粘贴方法(我的习惯)并加快编写文档的速度。
I want to know if this could be done.
I am building a data dictionary for our software system (school project), and I'm thinking of an automated way to do this. Basically I don't use much of Microsoft Word (2007), I only use it in documenting schools stuff, etc. I want to know if its possible to create/edit a Word document programmatically from a template.
The idea is, I will create a page on Word that contains an empty form that will be repeated on every page. For every data that I will input to my program, it will update the corresponding field in the form and skips to the next form.
The purpose of this, is to eliminate copy-paste methods (my habit) and to speed things up when doing the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如其他人所建议的,Word 自动化将导致您陷入痛苦的世界,原因有两个:
我使用过 Aspose.Words,它的成本有点高,但效果很好,就是为此而设计的。
Word automation, as suggested by others, will lead you to a world of hurt for two major reasons:
I have used Aspose.Words, it costs a little, but it works well and is intended for this.
不太确定您真正想要什么,但使用 c# 创建 Word 文档应该不会有任何问题:
http:// support.microsoft.com/kb/316384
Not exactly sure what you really want, but creating word documents with c# shouldn't be any problem:
http://support.microsoft.com/kb/316384
如果我正确地找到了您的目的,您需要访问此 microsoft msdn 链接
操纵 Word 2007 年 OpenXML 文件
If i find out your purpose correctly you need to visit this microsoft msdn link
Manipulating Word 2007 Files with OpenXML
绝对有可能。使用办公自动化来实现这一点相当简单。请参阅此知识库文章了解基本示例:如何使用 Visual C# 自动执行 Microsoft Word 创建新文档
我认为该示例的主要区别在于您将打开模板并执行“另存为”而不是创建新文档,但我记不清了。
但是,根据您的具体要求,可能有更好的选择。例如,不建议在服务器(包括网络服务器)上执行办公自动化,因此如果需要,您可能想看看其他东西。
Definitely possible. A fairly easy way of doing it using Office Automation. See this KB article for a basic sample: How to automate Microsoft Word to create a new document by using Visual C#
I think the main difference to that sample will be that you'll open your template and do
SaveAs
instead of creating a new document, but I can't remember exactly.However, depending on your exact requirements, there might be better alternatives. For example, it's not recommended to do Office Automation on servers (including on webservers), so if that's needed you might want to look at something else.
您可以使用.net框架的com interop。
从 .NET 开发人员的角度了解 Word 对象模型
在 C# 中构建 COM 对象< /a>
You can use com interop of .net framework.
Understanding the Word Object Model from a .NET Developer's Perspective
Building COM Objects in C#
使用 COM 编程并不是 erikkallen 提到的最好方法,我建议使用 OPEN XML。它真的很容易使用,并且您的文档生成操作将会非常快。
http://blog.goyello.com/2009/08/21/how-to-generate-open-xml-file-in-c-in-4-minutes/
http://msdn.microsoft.com/en-us/library/aa338205(v=office.12).aspx
Using COM programming is not the best way as mentioned by erikkallen, I suggest using OPEN XML. It is really easy to use and your document generation operation will be very fast.
http://blog.goyello.com/2009/08/21/how-to-generate-open-xml-file-in-c-in-4-minutes/
http://msdn.microsoft.com/en-us/library/aa338205(v=office.12).aspx