`
897371388
  • 浏览: 526594 次
文章分类
社区版块
存档分类
最新评论

c#操作word基本方法

 
阅读更多

http://qzone.qq.com/blog/75436328-1229874713

导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:

1.创建新Word
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
2.打开文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:/CCCXCXX/TestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
3.导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:/XXXCCX/Test.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
4..添加新表
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
5.设置表宽度和高度
//选中书签区域
oDoc.Bookmarks.Item(ref obj).Range.Select();
//定义表格
Word.Table table= application.Selection.Tables.Add(application.Selection.Range,3,columns,
ref missing, ref missing);
table.Select();
//设置表格行高度
table.Application.Selection.Rows.SetHeight(40f,Word.WdRowHeightRule.wdRowHeightExactly);
//设置表格列宽度
table.Application.Selection.Columns.SetWidth(120f,Word.WdRulerStyle.wdAdjustSameWidth);
//设置表格行间距
table.Application.Selection.ParagraphFormat.LineSpacing=10f;
//设置表格字体大小
table.Range.Font.Size=10;

6..表插入行
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

7.单元格合并
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
8.单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
9.通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "//endofdoc"; /**//* /endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();

分享到:
评论

相关推荐

    C#操纵Word的基本方法,完整函数

    C#操纵Word的基本方法 提供完整的基本操作函数

    C#操作Word详解

    C#操作word详解 基本覆盖了C#所有关于word操作 楼主潜心积累了很久才整理出来的,对于操作word的C#程序员绝对有用

    C# 操作Word

    非常好用的一个C#操作word的类库。能在word文档中任意位子写文字以及段落和表格(带线和不带线以及虚线)、图片、页眉页脚。word能操作的,这个类库基本都可以完成。下载直接使用。

    C#操作Word基本文档代码

    创建Word文档、设置Word文档格式、添加表格、添加图片、添加文本、Word书签使用、Word转Html

    word文档合并C#实现

    本程序演示C#如何完成word文档合并。 在此基础上演示如何在word文档中进行基本的读写的操作。

    c#操作word

    讲述了利用c#如何操作word的一些基本方法

    C#对Word的一系列操作

    C#对Word的一系列基本操作,一般的操作都有了

    C#读写Excel和Word文件

    实现C#读写Excel和Word文件基本操作,可以读写Word文件和读出Excel中的数据并实现Excel与数据库的导入与导出

    基于C#2008编写的word

    这是一个基于C# 2008编写的word文档,实现基本的word操作功能,方便大家参考

    C#开发实例大全(基础卷)

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程技术、...C#与Word互操作、高效应用Excel、基本图形绘制、图像处理技术、常用图表应用、动画处理技术、音频与视频控制...

    C# WinForm UI 设计方法

    这里主要提供了一种设计思路, 你可以将成套的UI图像添加或替换到本程序中。 在设计过程中,还存在许多疏漏,懒得仔细修改了,在使用过程中自行...2、提供尽量详细的操作说明和编辑、修改、添加说明(word文档37页),

    Word.zip_word

    c# 对word编程 使用的基本操作方法

    C#操作access

    VS2010,实现操作access数据库,实现基本连接,增、删,查,改操作。包含一个word说明文档

    Word操作类,包含word拆分

    实现word的基本操作。该类实现了根据字词,批量将word进行拆分功能。 实现了根据开始关键词到结束关键词之间内容的提取。 详细请看博客中油详细介绍及使用说明

    《C#开发实例大全(基础卷)》软件开发技术联盟

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程技术、...C#与Word互操作、高效应用Excel、基本图形绘制、图像处理技术、常用图表应用、动画处理技术、音频与视频控制...

    C#开发实战1200例+第1卷.part7

    内容简介 ...第19章 c#与word互操作 第20章 高效应用excel 第6篇 图形图像与多媒体篇 第21章 基本图形绘制 第22章 图像处理技术 第23章 常用图表应用 第24章 动画处理技术 第25章 音频与视频控制

    c#操作摄像头的文档资料

    受够了那些重复了不知道多少遍、注释不全、基本没有多少用处的代码了。...这是一份关于用C#语言对摄像头进行简单处理的教程,是一个word文档,包括图像获取、抓拍图片、录制影像、定时操作等基本功能

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程技术、...C#与Word互操作、高效应用Excel、基本图形绘制、图像处理技术、常用图表应用、动画处理技术、音频与视频控制...

    C# 程序设计手册(WORD)

    互操作性 518 使用平台调用播放 WAV 檔 520 范例 COM 类别 523 线程 524 使用线程 525 线程同步处理 526 建立和结束线程 530 同步处理产生者和消费者线程 534 使用线程集区 542 反映 545 C# DLL 547 建立和使用 C# ...

    C#开发实战1200例+第1卷

    内容简介 ...第19章 c#与word互操作 第20章 高效应用excel 第6篇 图形图像与多媒体篇 第21章 基本图形绘制 第22章 图像处理技术 第23章 常用图表应用 第24章 动画处理技术 第25章 音频与视频控制

Global site tag (gtag.js) - Google Analytics