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();
查看全文
相关阅读:
《深入理解Java虚拟机》笔记--第四章、虚拟机性能监控与故障处理工具
《深入理解Java虚拟机》笔记--第三章 、垃圾收集器与内存分配策略
《深入理解Java虚拟机》笔记--第二章、Java内存区域与内存溢出异常
Netty并发优化之ExecutionHandler
Netty框架入门
123
如何清理任务栏程序预览的历史记录?
三个安装,看VIP电影
WIN 10 COM surrogate 关闭
微软官方win10系统安装u盘启动盘制作
原文地址:https://www.cnblogs.com/top5/p/1684723.html
最新文章
Java面试专题-多线程(3)-原子操作
Java面试专题-多线程篇(2)- 锁和线程池
Java面试专题-多线程篇(1)
Java面试专题-集合篇(2)
Java面试专题-基础篇(1)
Springboot整合WebSocket实现网页版聊天,快来围观!
搞定万亿级MySQL海量存储的索引与分表设计实战
云原生之:蚂蚁Service Mesh大规模落地实践与展望
Redis 中哈希分布不均匀该怎么办
为什么Java程序会执行一段时间后跑的更快?
热门文章
Redis持久化怎么选?成年人从来不做选择!
IO和NIO的对比篇
StyleCop与MSBuild集成
SQL SERVER SUBSTRING ( expression ,start , length )函数
解决IIS/ASP.NET错误:你需知的4中服务器日志
JavaScript工具搜集
棱镜计划在背后推动云计算么
【Util】之——cookie
《深入理解Java虚拟机》笔记--第十三章、线程安全与锁优化
《深入理解Java虚拟机》笔记--第十二章、Java内存模型与线程
Copyright © 2011-2022 走看看