一、Word对象模型 (.Net Perspective)
五大对象
Application :代表Microsoft Word应用程序本身是Document和Selection的基类。通过Application的属性和方法,我们可以控制Word的大环境。
Document :代表一个Word文档,当你新建一个Word文档或者打开一个已有的Word文档,你将创建一个Document对象,该对象被加入到WordsDocuments Collection中。拥有焦点的Document称为ActiveDocument,可以通过Application对象的ActiveDocument属性获得当前文档对象
Selection :代表当前选中的区域(高亮),没有选中区域时代表光标点。它通常是高亮显示的(例如,你要改变一段文字的字体,你首先得选中这段文字,那么选中的这块区域就是当前文档的Selection对象所包含的区域)
Bookmarks :书签
1>书签一般有名字
2>Saved with the document,且文档关闭了之后书签继续存在
3>书签通常是隐藏的,但也可以通过代码设置其为可见
Range :代表一块区域,与Selection类似,不过一般不可见
1>包含一个起始位置和一个结束位置
2>它可以包含光标点,一段文本或者整个文档
3>它包含空格,tab以及paragraph marks
4>它可以是当前选中的区域,当然也可以不是当前选中区域
5>它被动态创建
6>当你在一个Range的末尾插入文本,这将扩展该Range
word文档对象的结构图(关于对象的详细使用,可以参考msdn api):
二、实例使用
1、创建word
创建Word 文档所使用的主要方法是通过微软公司提供的Microsoft Word X Object Library,其中X 为版本号。Word2010对应14.0, Word 2007 对应12.0,Word 2003 对应11.0。通过在项目中添加该组件,即可使用微软公司提供的方法创建相应版本的Word 文档。
在实例中我将所要生成word的格式设置为2003版本,如下: object format = MSWord.WdSaveFormat.wdFormatDocument;
新建一个webForm项目文件, Com组件中添加 Microsoft Word 12.0 Object Library,引用面板中多出Microsoft.Office.Core、Microsoft.Office.Interop.Word两个引用。
在类文件中添加应用如下:
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
前几篇博客已经讲述了word创建、格式设置、文本添加、图片添加、表格添加展示部分代码,下面就不在展示了,下面主要说一下书签的使用。
2.书签使用:
使用步骤:
1:建立word模板,并且在word中插入要用到的书签
2:c#方法中新建word操作类,并且打开硬盘中建立好的word模板
3:找到word模板中的书签,并在书签处写入要插入的数据
代码实现:
public void AddDocModel()
{
killWinWordProcess();
wordApp = new ApplicationClass();
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;
object missing = System.Reflection.Missing.Value;
object templateName = Application.StartupPath + @"\Report";//最终的word文档需要写入的位置
object ModelName = Application.StartupPath + @"\Report\ReportModel_Stand.doc";//word模板的位置
object count = 1;
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
wordDoc = wordApp.Documents.Open(ref ModelName, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing);//打开word模板
//在书签处插入文字
object oStart = "PatName";//word中的书签名
Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置
range.Text = "这里是您要输入的内容";//在书签处插入文字内容
//在书签处插入表格
oStart = "PatInfo";//word中的书签名
range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置
MSWord.Table tab_Pat = wordDoc.Tables.Add(range, 2, 4, ref missing, ref missing);//开辟一个2行4列的表格
tab_Pat.Range.Font.Size = 10.5F;
tab_Pat.Range.Font.Bold = 0;
tab_Pat.Columns[1].Width = 50;
tab_Pat.Columns[2].Width = 65;
tab_Pat.Columns[3].Width = 40;
tab_Pat.Columns[4].Width = 40;
tab_Pat.Cell(1, 1).Range.Text = "病历号";
tab_Pat.Cell(1, 2).Range.Text = "PatientNO";
tab_Pat.Cell(1, 3).Range.Text = "身高";
tab_Pat.Cell(1, 4).Range.Text = "Height";
tab_Pat.Cell(2, 1).Range.Text = "姓名";
tab_Pat.Cell(2, 2).Range.Text ="PatientName";
tab_Pat.Cell(2, 3).Range.Text = "体重";
tab_Pat.Cell(2, 4).Range.Text = "Weight";
//保存word
object format = WdSaveFormat.wdFormatDocument;//保存格式
wordDoc.SaveAs(ref templateName, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//关闭wordDoc,wordApp对象
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
// 杀掉winword.exe进程
public void killWinWordProcess()
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (System.Diagnostics.Process process in processes)
{
bool b = process.MainWindowTitle == "";
if (process.MainWindowTitle == "")
{
process.Kill();
}
}
}
word书签使用
小结:这几篇博客整理下来,对于Word在项目中的使用,有了一个大体的理解, 如果在此解决此类问题 就会变得相对简单 容易很多。
——部分内容来自网络
分享到:
相关推荐
C#操作Word——C#向Word文档中写入文字
C#得到word里面的书签,并向书签插入内容,将DateGridView内容作为表格插入到word书签中。
C#操作word,签入书签,图表,表格
C#打开Word文档定位到指定书签。效果:点击页面的帮助按钮,程序打开Word文档,定位到指定的帮助页。注意:必须配置好书签,并且在程序指定每个帮助按钮的名称为某个书签名。
此程序主要展示了如何把图片插入事先设定好的书签位置之中。 下载之后可以直接运行。
C#开发Android应用实战——使用Mono for Android和.NET/C#(移动与嵌入式开发技术)是一本很不错的书籍,但网上下载的多是不完整的。该版本是完整版。希望对大家有所帮助。
2、如何使用C#在Word文档中插入图片以及读取Word文档中的图片?3、提取word 文档中的图片信息;4、提取WORD中的所有InlineShape图片并保存成文件;5、C#编程实现动态生成Word文档{1、2};6、C#读取Word文档的详细...
C#操作word文档 C#实现Word中表格信息读取
C#操作Word.doc C#操作Word表格.doc C#操作WORD辅助类.txt c#操作word模板插入文字、图片及表格.doc C#操作Word在Word文档中插入图片.docx C#用word模板做文档.doc 收集的C#操作Word的各种代码。直接复制粘贴到程序...
将C#操作word书签模板的每一步都写的非常详细,读者完全可以按照步骤一步步实践,文中并附上代码。
一段C#操作word,如何进行查找和替换
C#开发Android应用实战——使用Mono for Android和.NETC#.rar
C# Web服务高级编程——使用.NET Remoting和ASP.NET创建Web服务 源代码
open xml 操作 word 书签open xml 操作 word 书签open xml 操作 word 书签open xml 操作 word 书签
C#程序设计课程作业——计算器小程序,C#期末作业
分享C#操作Word表格的常用方法总结,c#操作word的代码比较少
c# 操作word,包含word文档的打开,修改,删除,添加文本等
C#操作Word文档(根据模板生成文档、替换内容、插入表格等)