要使用C#操作word,首先要添加引用:
1、添加引用->COM->Microsoft Word 11.0 Object Library
2、在.cs文件中添加
using Word;
using NewWord = Microsoft.Office.Interop.Word;
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:
(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)
public string CreateWordFile(string SaveFile)
{
string message = "";
try
{
Object Nothing = System.Reflection.Missing.Value;
object filename = SaveFile; //文件保存路径
//创建Word文档
NewWord.Application WordApp = new NewWord.ApplicationClass();
NewWord.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//添加页眉
WordApp.ActiveWindow.View.Type = NewWord.WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = Ne