博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 操作word (转)
阅读量:4514 次
发布时间:2019-06-08

本文共 3977 字,大约阅读时间需要 13 分钟。

前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:
创建新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);
打开文档:
            
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);
导入模板
            
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);
.添加新表
            
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);
.表插入行
            
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);
.单元格合并
            
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
 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);
            
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);
通过段落控制插入
            
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();

转载于:https://www.cnblogs.com/lf6112/archive/2011/06/07/2074431.html

你可能感兴趣的文章
Docker 安装及问题处理
查看>>
正则表达式之 数据验证 与 文本替换
查看>>
Visual Studio 2008 不能更改安装目录的原因
查看>>
关于求最大公约数
查看>>
Test指令
查看>>
[置顶] 怎么对待重复的代码
查看>>
多种方法实现H5网页图片动画效果;
查看>>
Ubuntu/CentOS下使用脚本自动安装 Docker
查看>>
源码解读Mybatis List列表In查询实现的注意事项
查看>>
POJ 2311 Cutting Game(二维SG+Multi-Nim)
查看>>
小强的HTML5移动开发之路(16)——神奇的拖放功能
查看>>
zookeeper FastLeaderElection
查看>>
进度条
查看>>
用户画像
查看>>
HTTP报文(面试会问开发时常用的报文头格式)
查看>>
机器学习从业人员到底做什么?
查看>>
word发表博客的方法
查看>>
Programming Erlang_CHAPTER2_Basic Erlang 学习笔记(2)。
查看>>
Linux基础
查看>>
【模板】高精度
查看>>