下图是Word对像模型

Application :用来表现WORD应用程序,包含其它所有对象。他的成员经常应用于整个WORD,你可以用它的属性和方法控制WORD环境。
Document :Document对象是WORD编程的核心。当你打开打开一个已有的文档或创建一个新的文档时,就创建了一个新的Documnet对象, 新创建的Document将会被添加到Word Documents Collection。
Selection :Selection对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。
Rang :是Document的连续部分,根据起始字符和结束字符定义位置。
Bookmark:类似于Rang,但Bookmark可以有名字并在保存Document时Bookmark也被保存。

以下代码则为打开一个WORD2003文件。

public void CreateWordDocument(string FileName)
        {
            if(FileName == "") return;
            this.thisApplication =
                new Microsoft.Office.Interop.Word.ApplicationClass();
            thisApplication.Visible = true;
            thisApplication.Caption = "";
            thisApplication.Options.CheckSpellingAsYouType = false;
            thisApplication.Options.CheckGrammarAsYouType = false;

Object filename = FileName;
            Object ConfirmConversions = false;
            Object ReadOnly = true;
            Object AddToRecentFiles = false;

Object PasswordDocument = System.Type.Missing;
            Object PasswordTemplate = System.Type.Missing;
            Object Revert = System.Type.Missing;
            Object WritePasswordDocument = System.Type.Missing;
            Object WritePasswordTemplate = System.Type.Missing;
            Object Format = System.Type.Missing;
            Object Encoding = System.Type.Missing;
            Object Visible = System.Type.Missing;
            Object OpenAndRepair = System.Type.Missing;
            Object DocumentDirection = System.Type.Missing;
            Object NoEncodingDialog = System.Type.Missing;
            Object XMLTransform = System.Type.Missing;

//            Microsoft.Office.Interop.Word.DocumentClass wordDoc =
//                wordApp.Documents.Open(ref filename, ref ConfirmConversions,
//                ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
//                ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
//                ref Encoding, ref Visible);
//            Microsoft.Office.Interop.Word.DocumentClass wordDoc =
//                wordApp.Documents.Open(ref filename, ref ConfirmConversions,
//            ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
//            ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
//            ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog);

Microsoft.Office.Interop.Word.Document wordDoc =
                thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
                ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
                ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
                ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
                ref NoEncodingDialog, ref XMLTransform );

this.thisDocument = wordDoc;

formFields = wordDoc.FormFields;
        }

关闭WORD程序程序
Object SaveChanges = false;
Object OriginalFormat = System.Type.Missing;
Object RouteDocument = System.Type.Missing;
this.thisApplication.Quit( ref SaveChanges, ref OriginalFormat, ref RouteDocument );

参考资料:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/wordobject.asp

上一篇讲到,一个Document可能会有多个Rang对象。Rang由起始和结束字符来定他的位置。
以下代码为先清空Document里的内容,再在第一行写入内容。
    // Clear out any existing information.
    Object start = Type.Missing;
    Object end = Type.Missing;
     Object unit = Type.Missing;
    Object count = Type.Missing;
    ThisDocument.Range(ref start, ref end). Delete(ref unit, ref count);

// Set up the header information.
    start = 0;
    end = 0;
     rng = ThisDocument.Range(ref start, ref end);
     rng.InsertBefore("Xiaopai");
     rng.Font.Name = "Verdana";
     rng.Font.Size = 16;
     rng.InsertParagraphAfter();//输入回车

以下为在刚写入的内容后添加一个表格。
    object missingValue = Type.Missing;
    object location = 8; //注:若location超过已有字符的长度将会出错。
    Word.Range rng = ThisDocument.Range(ref location, ref location);
    ThisDocument.Tables.Add(rng, 3, 4, ref missingValue, ref missingValue);

以下为在刚创建的表格里添加一行
    Word.Table tbl = ThisDocument.Tables[1]; //第一个表格为1,而不是0
    Object beforeRow = Type.Missing;
    tbl.Rows.Add(ref beforeRow); //在表格的最后添加一行

填充表格内容
    tbl.Cell(1, 1).Range.Text = "shuai"; //在表格的第一行第一列填入内容。

设置单元格风格
    Word.Range rngCell;
    rngCell = tbl.Cell(1, 2).Range;
    rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    rngCell.Font.Size = 8;
    rngCell.Font.Name = "Verdana";

当时没找到合并单元格的方法。有谁知道的共享一下哈。

参考资料:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtocreatewordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtoaddrowscolumnstowordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/odc_VSTWordtbl.asp

c#操作word文档(转自小排_流浪狗)相关推荐

  1. python操作word文档(python-docx)

    python操作word文档(python-docx) 1. 效果图 1.1 python-docx文档标题段落(等级.加粗.斜体.居中)效果图 1.2 python-docx字体(加粗.斜体.居中. ...

  2. ASP.NET操作Word文档(转)

    ASP.NET操作Word文档(转) 操作WORD配置说明 引入:Word的对象库文件"MSWORD.OLB"(word 2000为MSWORD9.OLB) 1.运行Dcomcnf ...

  3. Python 操作Word文档插入图片和表格实例演示

    Python 操作Word文档插入图片和表格实例演示 效果图 实现过程 ① python-docx 库安装 ② word 文档插入图片演示 ③ word 文档插入表格演示 [ 文章推荐 ] Pytho ...

  4. C#操作word文档(二)

    1.C#操作Word完全功略 导入COM库:Microsoft word 11.0 Object Library. 引用里面就增加了: 创建新Word               object oMi ...

  5. python操作word文档中的图片_Python操作word文档插入图片和表格的实例演示

    前言P6Q免费资源网 图片是Word的一种特殊内容,这篇文章主要介绍了关于Python操作word文档,向里面插入图片和表格的相关内容,下面话不多说了,来一起看看详细的代码P6Q免费资源网 实例代码: ...

  6. Java操作Word文档

    Java 生成 Word 的几种方案 参考 ​ 主要有这么一些工具可以使用 Jacob.Apache POI.Java2word.iText,还有一种方法是使用XML作为模板. ​ 使用 XML 的思 ...

  7. Spire操作word文档

    Spire操作word文档 1.基本文档方面 // 测试用例PersonalCreditRecord credit = getPersonalCreditRecord();//Create word ...

  8. java使用jacob操作word文档

    ava使用jacob操作word文档 java调用com组件操作word使用总结(jacob) 简单描述 在此处输入简单摘要 特别声明:使用java-com技术可以完成任何VBA可以完成的office ...

  9. python打开word并插入图片_Python操作word文档插入图片和表格的实例演示

    前言 图片是Word的一种特殊内容,这篇文章主要介绍了关于Python操作word文档,向里面插入图片和表格的相关内容,下面话不多说了,来一起看看详细的代码 实例代码: # -*- coding: U ...

最新文章

  1. Python性能测试
  2. C++coin change 硬币找零(附完整源码)
  3. [原]一步一步自己制作弹出框
  4. android 日程安排view,RecyclerView 列表控件中简单实现时间线
  5. [机器学习-概念篇]彻底搞懂信息量,熵、相对熵、交叉熵
  6. python中数据类型转换_Python(二).数据类型,数据类型转换
  7. linux下的asp.net服务器,Linux(Ubuntu)下搭建ASP.NET Core环境
  8. Intellij IDEA 安装插件 报 ‘plugin xxxx is incompatible‘ 解决方案
  9. 探索性因子分析和验证性因子分析有什么区别?
  10. 解决 Kotlin 换页符提示错误 Illegal escape f 无法使用问题
  11. Vmware Vsphere HA
  12. Viusal 各个版本离线镜像
  13. EagleEye的特性分析
  14. 每日一坑:加载DLL失败:找不到指定模块
  15. 验证sqlserver 2000 sp4补丁是否安装成功(安装补丁后可以远程访问)
  16. supervisor+cesi多服务器进程集中管理
  17. 微信小程序消息推送,实现未完成计划的在微信内的定时提醒功能
  18. 2018年12月8日国际项目经理PMP培训考试报名中
  19. Linux awk 命令详解
  20. SpiffWorkflow定制工作流

热门文章

  1. 阿里达摩院最新FEDformer,长程时序预测全面超越SOTA | ICML 2022
  2. H5数独游戏开发——游戏通关及重玩
  3. bugkuctf 游戏通关玄学式速通
  4. MEDICI仿真NMOS器件晶体管语法笔记
  5. win10照片查看器_Win10 下好用的免费无广告看图软件 XnView
  6. 怎样查询自己正在上网的路由器的IP地址?网络节点跟踪命令tracert的用法
  7. 前缀树TrieNode
  8. 苹果“传奇”追授总统自由勋章——史蒂夫·乔布斯,未来还会有吗
  9. Android模拟器的ip获取以及模拟器之间socket通信
  10. 网页的兼容性解决办法