C#中的word编程-基本操作(1)
2010-06-03 17:14
387 查看
1、
运行环境:Windows XP
,Visual Studio.NET 2005
,Office 2003
在菜单栏选择“
项目”
-“
添加引用”
,弹出的窗口中我们可以选择“COM”
选项卡,导入COM
库:Microsoft word 11.0 Object Library.
2、
word
文档基本操作
(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);
函数原型:oWord.Documents.Add(ref
object Template, ref
object
NewTemplate, ref
object DocumentType, ref
object Visible)
(2
)打开word
文档:
object
oMissing = System.Reflection.Missing
.Value;
Word._Application
oWord;
Word._Document
oDoc;
oWord = new
Word.Application
();
oWord.Visible = true
;
object fileName =
@"D:/a.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);
函数原型:oWord.Documents.Open
(ref
object
fileName, ref
object
ConfirmConversions,
ref
object
ReadOnly, ref
object
AddToRecentFiles, ref
object
PasswordDocument,
ref
object
PasswordTemplate, ref
object
Revert, ref
object
WritePasswordDocument,
ref
object
WritePasswordTemplate, ref
object
Format, ref
object
Encoding,
ref
object
Visible, ref
object
OpenAndRepair, ref
object
DocumentDirection,
ref
object
NoEncodingDialog, ref
object
XMLTransform);
(3
)导入word
模板:
object
oMissing = System.Reflection.Missing
.Value;
Word._Application
oWord;
Word._Document
oDoc;
oWord = new
Word.Application
();
oWord.Visible = true
;
object fileName =
@"
D:/a.doc
";
oDoc = oWord.Documents.Add(ref
fileName, ref oMissing,
ref oMissing,
ref oMissing);
(4
)关闭word
:
关闭已打开的直接用:
oDoc.Save();
关闭不保存:
object
SaveChanges = false
;
//
是否保存更改 true:
保存
false:
不保存
//
关闭文档
oDoc.Close(ref
SaveChanges, ref
oMissing, ref
oMissing);
//
退出word
程序
oWord.Quit(ref
SaveChanges, ref
oMissing, ref
oMissing);
另存为:
object
filename = "C://Documents
and Settings//Administrator//
桌面//a.doc"
;
oDoc.SaveAs(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、
表格操作
(1)
添加表格
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);
(2)
表插入行
Word.Table
newTable = oDoc.Tables[1];
//
把文档对象的第一个表格赋给newTable
object
beforeRow = newTable.Rows[3];
newTable.Rows.Add(ref
beforeRow);
(3)
表插入列
Word.Table
newTable = oDoc.Tables[1];
//
把文档对象的第一个表格赋给newTable
object
beforeColumn = newTable.Columns[1];
newTable.Columns.Add(ref
beforeColumn);
(4)
合并单元格
Word.Cell
cell = newTable.Cell(2, 2);
//Cell(int row,int column)
第几行几列,选中单元格赋给cell
cell.Merge(newTable.Cell(3,
2));
//
合并cell
和Cell(3,2)
,即3
行2
列的单元格
(5)
拆分单元格
object
Rownum = 4;
object
Columnnum = 2;
cell.Split(ref
Rownum, ref
Columnnum);
//
将cell
拆分成Rownum
行Columnnum
列
(6)
填充单元格
newTable.Cell(2, 1).Range.Text = "
要填充的内容"
;
运行环境:Windows XP
,Visual Studio.NET 2005
,Office 2003
在菜单栏选择“
项目”
-“
添加引用”
,弹出的窗口中我们可以选择“COM”
选项卡,导入COM
库:Microsoft word 11.0 Object Library.
2、
word
文档基本操作
(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);
函数原型:oWord.Documents.Add(ref
object Template, ref
object
NewTemplate, ref
object DocumentType, ref
object Visible)
(2
)打开word
文档:
object
oMissing = System.Reflection.Missing
.Value;
Word._Application
oWord;
Word._Document
oDoc;
oWord = new
Word.Application
();
oWord.Visible = true
;
object fileName =
@"D:/a.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);
函数原型:oWord.Documents.Open
(ref
object
fileName, ref
object
ConfirmConversions,
ref
object
ReadOnly, ref
object
AddToRecentFiles, ref
object
PasswordDocument,
ref
object
PasswordTemplate, ref
object
Revert, ref
object
WritePasswordDocument,
ref
object
WritePasswordTemplate, ref
object
Format, ref
object
Encoding,
ref
object
Visible, ref
object
OpenAndRepair, ref
object
DocumentDirection,
ref
object
NoEncodingDialog, ref
object
XMLTransform);
(3
)导入word
模板:
object
oMissing = System.Reflection.Missing
.Value;
Word._Application
oWord;
Word._Document
oDoc;
oWord = new
Word.Application
();
oWord.Visible = true
;
object fileName =
@"
D:/a.doc
";
oDoc = oWord.Documents.Add(ref
fileName, ref oMissing,
ref oMissing,
ref oMissing);
(4
)关闭word
:
关闭已打开的直接用:
oDoc.Save();
关闭不保存:
object
SaveChanges = false
;
//
是否保存更改 true:
保存
false:
不保存
//
关闭文档
oDoc.Close(ref
SaveChanges, ref
oMissing, ref
oMissing);
//
退出word
程序
oWord.Quit(ref
SaveChanges, ref
oMissing, ref
oMissing);
另存为:
object
filename = "C://Documents
and Settings//Administrator//
桌面//a.doc"
;
oDoc.SaveAs(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、
表格操作
(1)
添加表格
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);
(2)
表插入行
Word.Table
newTable = oDoc.Tables[1];
//
把文档对象的第一个表格赋给newTable
object
beforeRow = newTable.Rows[3];
newTable.Rows.Add(ref
beforeRow);
(3)
表插入列
Word.Table
newTable = oDoc.Tables[1];
//
把文档对象的第一个表格赋给newTable
object
beforeColumn = newTable.Columns[1];
newTable.Columns.Add(ref
beforeColumn);
(4)
合并单元格
Word.Cell
cell = newTable.Cell(2, 2);
//Cell(int row,int column)
第几行几列,选中单元格赋给cell
cell.Merge(newTable.Cell(3,
2));
//
合并cell
和Cell(3,2)
,即3
行2
列的单元格
(5)
拆分单元格
object
Rownum = 4;
object
Columnnum = 2;
cell.Split(ref
Rownum, ref
Columnnum);
//
将cell
拆分成Rownum
行Columnnum
列
(6)
填充单元格
newTable.Cell(2, 1).Range.Text = "
要填充的内容"
;
相关文章推荐
- C#网络编程(基本概念和操作) - Part.1
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#网络编程(基本概念和操作) - Part.1
- c#之Word基本操作----创建,打开,绘制表格,插入图片等
- C#网络编程(基本概念和操作) - Part.1
- C#操作word中的表格,最基本的读写操作
- 踏踏实实学编程 基于C#操作Word文档中的Bookmark
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- [转]C#网络编程(基本概念和操作) - Part.1
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#网络编程(基本概念和操作) - Part.1
- c#操作word基本方法
- (转)C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1
- C#网络编程(基本概念和操作) - Part.1