c# 操作Word总结(四)——书签使用

一、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在项目中的使用,有了一个大体的理解, 如果在此解决此类问题 就会变得相对简单 容易很多。

——部分内容来自网络







  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值