zoukankan
html css js c++ java
C#操作Word完全功略
前提:
导入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,
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();
查看全文
相关阅读:
SharePoint 2013中的Index Partition的一个小问题
SharePoint 2013中, 默认Index文件的位置
Visual Studio Test Project的一个小问题
HyperV最佳实践
测试环境中的一个HyperV的选项设置
什么是SharePoint 2013中的Shredded Storage?
SharePoint的数据库性能需要注意的一点
记录HyperV中挪动虚拟机的一次实践
SharePoint 2013上一台机器可以有多个Crawl Component么?
SharePoint Client Object Model的一个小例子
原文地址:https://www.cnblogs.com/top5/p/1684723.html
最新文章
端口号是什么概念
在web.config和app.config文件中增加自定义配置节点
Windows XP 桌面不見
C#索引器
C#基础知识梳理系列十四:序列化
Windows XP中安装虚拟网卡microsoft loopback adapter
win7禁用休眠的方法 节省C盘空间
C#的调试和错误处理
C# 3.0下有限状态机的一种优雅的实现
C# IEnumerable<T>、IEnumerator<T>、List<T>、ArrayList、[]数组各各的区别
热门文章
这就是电脑给我的情人节礼物
五子棋的核心算法
BT种子文件 bencoding编码详细解析
J2EE全面介绍
原来我的综合素质那么低,自评一下你能达到几个?
玩转Autorun.inf
lucene.net 应用资料
三级 考后
[导入]十大经典误会
今天体育课受伤
Copyright © 2011-2022 走看看