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();

  • 相关阅读:
    Individual Method Selection Survey rubric
    Xcode 出现Thread 1: signal SIGABRT
    C/C++生成随机数
    《深入浅出深度学习:原理剖析与python实践》第八章前馈神经网络(笔记)
    操作系统--精髓与设计原理(第八版)第三章复习题答案
    操作系统--精髓与设计原理(第八版)第二章复习题答案
    Python知识点整理
    C++ <queue>用法
    C语言结构体用法
    Mac安装Qt出现错误Could not resolve SDK Path for 'macosx'
  • 原文地址:https://www.cnblogs.com/lenovo_tiger_love/p/1664260.html
Copyright © 2011-2022 走看看