c#操作word基本方法
来源:互联网 发布:linux开机运行脚本 编辑:程序博客网 时间:2024/05/21 22:35
http://qzone.qq.com/blog/75436328-1229874713
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:
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);
2.打开文档:
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);
3.导入模板
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);
4..添加新表
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);
5.设置表宽度和高度
//选中书签区域
oDoc.Bookmarks.Item(ref obj).Range.Select();
//定义表格
Word.Table table= application.Selection.Tables.Add(application.Selection.Range,3,columns,
ref missing, ref missing);
table.Select();
//设置表格行高度
table.Application.Selection.Rows.SetHeight(40f,Word.WdRowHeightRule.wdRowHeightExactly);
//设置表格列宽度
table.Application.Selection.Columns.SetWidth(120f,Word.WdRulerStyle.wdAdjustSameWidth);
//设置表格行间距
table.Application.Selection.ParagraphFormat.LineSpacing=10f;
//设置表格字体大小
table.Range.Font.Size=10;
6..表插入行
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);
7.单元格合并
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));
8.单元格分离
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);
9.通过段落控制插入
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();
- c#操作word基本方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C# 操作word常用方法
- C#操作Word完全方法
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- 利用vb操作word的基本方法
- C#操作Word完全方法(转)
- C#操作word中的表格,最基本的读写操作
- C#中的word编程-基本操作(1)
- c# word替换(Find.Excute方法)操作 去除空行方法
- c# word替换(Find.Excute方法)操作 去除空行方法
- 国内3G时代的来临
- 网路层设计要点
- uservoice让你看到用户对你产品的真实需求
- 用php写的一个HTTP下载类
- Flex 中的元数据标签
- c#操作word基本方法
- 通过反射来判断某个程序集中是否有实现该接口的类
- 增强自己的独立技术开发能力
- 不用iconv函数实现UTF-8编码转换GB2312的PHP函数
- C#操作word例程之二
- 兼容性最强的PHP生成缩略图的代码
- preg_replace比ereg_replace快多少?
- How to install GD/Curl in cPanel server
- AMD与Nvidia显卡发展策略