C#操作Word完全方法
来源:互联网 发布:免费域名加解析 编辑:程序博客网 时间:2024/05/01 03:08
前提:引入COM组件
1、创建
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(refoMissing, refoMissing, refoMissing, refoMissing);
2、打开
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
objectfileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(reffileName,
refoMissing, refoMissing, refoMissing, refoMissing, refoMissing,
refoMissing, refoMissing, refoMissing, refoMissing, refoMissing,
refoMissing, refoMissing, refoMissing, refoMissing, refoMissing);
3、导入模板
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
objectfileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(reffileName, refoMissing, refoMissing, refoMissing);
4、添加新表
objectoMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = newWord.Application();
oWord.Visible = true;
objectfileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(reffileName, refoMissing, ref oMissing,ref oMissing);
5、表插入行
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);
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);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
7、单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(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);
8、通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = @"\endofdoc";
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);
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;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完全功略!
- C#操作Word完全功略!
- C#操作Word完全功略
- C#操作Word完全功略
- C#操作Word完全功略!
- C#操作Word完全功略!
- C#操作Word完全功略!
- c#操作word基本方法
- 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数
- UNIX网络编程——TCP输出,UDP输出
- dedecms的利与弊
- 神经网络常用函数
- hdu1253
- C#操作Word完全方法
- softmax 函数
- hdu-又见GCD
- 关于fedora 16默认文本界面启动
- “绿色中国梦”爱心活动
- 再谈PHP、Python与Ruby
- Candy----HDU4465----数学题
- UML相关工具一览(截止2013年2月)
- POJ 2528 Mayor’s posters(离散化+成段更新)