zoukankan      html  css  js  c++  java
  • C#操作Word完全方法

    前提:引入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();

  • 相关阅读:
    SQL Server 添加数据库没有权限等
    网站图片优化的重要性与技巧方案
    5年前端经验小伙伴教你纯css3实现饼状图
    css3 斜切角/斜边的实现方式来自BAT大神的出品
    Validate表单验证插件之常用参数介绍
    html实现邮箱发送邮件_js发送邮件至指定邮箱功能
    css重设样式_清除浏览器的默认样式
    大厂前端工程师教你如何使用css3绘制任意角度扇形+动画
    WordPress教程之如何批量删除未引用(无用)的TAG标签
    css引入的方式有哪些_四种css的引入方式与特点
  • 原文地址:https://www.cnblogs.com/lenovo_tiger_love/p/1664260.html
Copyright © 2011-2022 走看看