使用C#自动编辑Word文档 [英] Automatically edit word documents using C#

查看:74
本文介绍了使用C#自动编辑Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存在的Word文档,FinalReport.docx,带有自己的模板,实际上是我手动编辑的:标题,某些段落和某些表的内容取自其他Matlab文档,例如file1.m,file2.m等最后,我需要更新FinalReport索引.我想自动执行此操作,可能使用 C# 或 VBA.有什么建议吗?

I have an exisent word document, FinalReport.docx with its own template, which I actually edit manually: the title, some paragraphs and the content of some tables are taken from other Matlab documents, let's say file1.m, file2.m, etc. Finally I need to update the FinalReport index. I'd like to do this automatically, maybe using C# or VBA. Any suggestions?

推荐答案

您可以使用Microsoft.Office.Interop.Word命名空间编辑Word文档.在Google上查找有关该主题的教程(这是一个).它要求您已安装MS Word,如果您首先在同一台计算机上完成手动步骤,那么我认为这不是问题.它使用COM接口来控制Word.

You can edit Word documents with the Microsoft.Office.Interop.Word namespace. Google that to find tutorials on the subject (here's one). It requries that you have MS Word installed, which I don't think is a problem if you are first completing a manual step on the same computer. It uses a COM interface to control Word.

对于像这样的简单事情,它比Open XML SDK容易,但对于高级操作(例如,在服务器上创建报告),Open XML SDK会更好.而且SDK不需要您安装MS Word.

It's easier than the Open XML SDK for simple things like this, but the Open XML SDK will work better for advanced operations (creating reports on a server, for example). And the SDK doesn't require you to install MS Word.

VBA也可以用来执行此操作,这对于初学者来说更好,因为您可以使用宏记录器 让您开始,然后手动编辑它生成的 VBA 以添加您无法使用宏记录器.

VBA can be used to do this also, which would be better for a beginner, because you can use the macro recorder to get you started, then manually edit the VBA it generates to add things you can't do with the macro recorder.

我之前已经完成了所有这三个操作.如果这是您第一次自动执行MS Word任务,建议您从VBA开始.

I've done all three of these before. If this is your first experience automating MS Word tasks, I recommend you start with VBA.

这篇关于使用C#自动编辑Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆