- C#也能动态生成Word文档并填充数据
-
要使用C#操作word,首先要添加引用:
1、添加引用->COM->Microsoft Word 11.0 Object Library
2、在.cs文件中添加
usingWord;
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)

publicstringCreateWordFile(stringCheckedInfo)

...{
stringmessage="";
try

...{
ObjectNothing=System.Reflection.Missing.Value;
Directory.CreateDirectory("C:/CNSI");//创建文件所在目录
stringname="CNSI_"+DateTime.Now.ToShortString()+".doc";
objectfilename="C://CNSI//"+name;//文件保存路径
//创建Word文档
Word.ApplicationWordApp=newWord.ApplicationClass();
Word.DocumentWordDoc=WordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);

//添加页眉
WordApp.ActiveWindow.View.Type=WdViewType.wdOutlineView;
- 与本文主题相关的文章
