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

  • 相关阅读:
    HTTP和HTTPS的区别
    python计算机二级考试知识点——文件操作
    python二级考试知识点——turtle、random、time、PyInstaller、jieba、wordcloud
    淘宝搜索功能的测试
    百度搜索测试用例
    微信朋友圈测试用例
    微信聊天功能测试用例
    微信红包测试用例
    微信点赞功能测试用例
    SQL Server 远程共享文件夹备份
  • 原文地址:https://www.cnblogs.com/lenovo_tiger_love/p/1664260.html
Copyright © 2011-2022 走看看