C#中的word编程-基本操作(1)
来源:互联网 发布:windows tmg ipsec 编辑:程序博客网 时间:2024/06/07 07:13
1、 运行环境:Windows XP,Visual Studio.NET 2005,Office 2003
在菜单栏选择“项目”-“添加引用”,弹出的窗口中我们可以选择“COM”选项卡,导入COM库:Microsoft word 11.0 Object Library.
2、 word文档基本操作
(1)新建word文档:
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, refoMissing, ref oMissing, ref oMissing);
函数原型:oWord.Documents.Add(ref object Template, ref objectNewTemplate, ref object DocumentType, ref object Visible)
(2)打开word文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
object fileName =@"D:/a.doc";
oDoc = oWord.Documents.Open(reffileName, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing, 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 = newWord.Application();
oWord.Visible = true;
object fileName =@"D:/a.doc";
oDoc = oWord.Documents.Add(reffileName, ref oMissing, ref oMissing,ref oMissing);
(4)关闭word:
关闭已打开的直接用: oDoc.Save();
关闭不保存:
object SaveChanges = false;//是否保存更改 true:保存false:不保存
//关闭文档
oDoc.Close(ref SaveChanges, refoMissing, ref oMissing);
//退出word程序
oWord.Quit(refSaveChanges, ref oMissing, ref oMissing);
另存为:
object filename = "C://Documentsand Settings//Administrator//桌面//a.doc";
oDoc.SaveAs(reffilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, refoMissing);
3、 表格操作
(1) 添加表格
object start = 0;
objectend = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, refoMissing, ref oMissing);
(2) 表插入行
Word.TablenewTable = oDoc.Tables[1]; //把文档对象的第一个表格赋给newTable
objectbeforeRow = newTable.Rows[3];
newTable.Rows.Add(ref beforeRow);
(3) 表插入列
Word.TablenewTable = oDoc.Tables[1]; //把文档对象的第一个表格赋给newTable
objectbeforeColumn = newTable.Columns[1];
newTable.Columns.Add(ref beforeColumn);
(4) 合并单元格
Word.Cellcell = 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;
objectColumnnum = 2;
cell.Split(refRownum, ref Columnnum); //将cell拆分成Rownum行Columnnum列
(6) 填充单元格
newTable.Cell(2, 1).Range.Text = "要填充的内容";
- C#中的word编程-基本操作(1)
- C#操作word中的表格,最基本的读写操作
- c#操作word基本方法
- 踏踏实实学编程 基于C#操作Word文档中的Bookmark
- C#操作Word (1)Word对象模型
- C#操作Word (1)Word对象模型
- C# 操作Word入门-1
- C#操作WORD(一)
- C#操作WORD(二)
- C# Word操作(转载)
- VS2010 操作Word(二)基本操作
- c#经典编程实例(ado.net基本操作)
- C#网络编程(二、Socket基本操作)
- Word基本操作
- word基本操作
- C#的office word文档的操作(1)
- C#中操作Word(3)—— 利用Range对象读取和设置Word中的文本
- Linux内核中的一些基本编程操作
- 创建物料BOM的fm
- 回发或回调参数无效
- Android,UI主线程与子线程
- 从PC总线的发展看设备驱动程序的发展
- socket
- C#中的word编程-基本操作(1)
- Linux和英语资源链接
- Div+Css+JS做弹出窗口
- JAVA调用CMD命令
- android中Message机制的灵活应用
- Windows下Eclipse环境搭建
- WORKING WITH SQLite DATABASES
- dedecms与uchome整合问题
- Apache服务器实现用户验证