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();
查看全文
相关阅读:
Shell基础:什么是shell脚本、2种脚本解释器、#!约定解释器类型、运行shell脚本的2种方式、shell变量命令规范/赋值/如何使用/只读变量/删除变量/变量类型、shell字符串及其常用方法、shell数组及其常用方法、shell注释
Linux su命令:su命令语法、su root与su
docker容器内使用apt报错E: List directory /var/lib/apt/lists/partial is missing.
浅析事务是什么、mysql是如何实现事务提交和回滚的、保证事务持久性redo log的实现原理、保证事务一致性undo log的实现原理、事务ACID特性及其实现原理
浅析前后端分离架构下的API安全问题:JWT保证token不被盗用的方案(即如何防范Replay Attacks)
浅析如何保证缓存与数据库的双写一致性:4种更新缓存的设计模式理解
浅析SpringCloud中断路器是什么、断路器的作用以及在Feign中使用断路器
浅析后端微服务涉及到定时任务时如何解决多集群定时任务重复执行并发的方案对比
Linux连续执行多条命令的写法区别
Dockerfile中RUN/CMD/ENTRYPOINT命令区别
原文地址:https://www.cnblogs.com/top5/p/1684723.html
最新文章
nginx 文档
nginx启动时出现Unit nginx.service is masked.
MySQL查看表占用空间大小
阿里云服务器rds 数据库连接数比较高
Numpy 学习 第五篇:数组的高级操作
Maven实现多module打包成单个jar
Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)
利用位运算实现一个字段表示多个属性
Mysql String 转int 比较
解析json之net.sf.json
热门文章
在阻塞式io中,如果一个线程在等待io操作,那么cpu还会分配时间片给该线程吗?
详解Condition的await和signal等待通知机制
wait和notify的理解与使用
关于Thread.sleep的几个问题
培训也需要设计——你们是如何做培训的呢?
不要让“破事”、“烂人”毁了你的工作计划
聊聊【向上管理】中的“尺度”
你有没有乱用“leader”,担当是个好东西
你为什么不想向上汇报?
复盘机制如何在新团队落地?
Copyright © 2011-2022 走看看