C#通过COM组件对WORD文档操作

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using Microsoft.Office.Core;
  5. using Word;
  6. namespace ReadXML
  7. {
  8.     /// <summary>
  9.     /// 对于Word文档操作的类
  10.     /// 通过COM组件来对Word进
  11.     /// 行读取并存储到字符串中
  12.     /// </summary>
  13.     class myWord
  14.     {
  15.         Word.Application app = new Application();   //可以打开word程序
  16.         Word.Document doc = null//实例化一个word文档
  17.         string temp = "";
  18.         /// <summary>
  19.         /// 打开一个Word文档
  20.         /// </summary>
  21.         /// <param name="fileName">输入参数为文档路径</param>
  22.         public void openWord(object fileName)
  23.         {
  24.             try
  25.             {
  26.                 if (app.Documents.Count > 0)
  27.                 {
  28.                         object unknow = Type.Missing;
  29.                         doc = app.ActiveDocument;          //激活文档
  30.                         app.ActiveDocument.Save();
  31.                       
  32.                         app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
  33.                         app.Visible = false;
  34.                 }
  35.             }
  36.             catch (Exception ex)
  37.             {
  38.                 Console.WriteLine(ex.Message);
  39.                 Console.ReadLine();                                                                            //捕获错误并显示
  40.                 app = new Word.Application();
  41.             }
  42.             try
  43.             {
  44.                 object unknow = Type.Missing;
  45.                 app.Visible = true;                                                                                 //是否显示Word程序
  46.                 doc=app.Documents.Open(ref fileName,
  47.                                                          ref unknow, ref unknow, ref unknow, ref unknow,
  48.                                                          ref unknow, ref unknow, ref unknow, ref unknow,
  49.                                                          ref unknow, ref unknow, ref unknow, ref unknow,
  50.                                                          ref unknow, ref unknow, ref unknow);
  51.                 
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 Console.WriteLine(ex.Message);                                                      //捕获错误并显示
  56.                 Console.ReadLine();
  57.             }
  58.         }
  59.         /// <summary>
  60.         /// 根据段落数目读取并存入字符串中
  61.         /// </summary>
  62.         public void readPar()
  63.         {
  64.             try
  65.             {
  66.                 int paraCount = doc.Paragraphs.Count;                                         //获取文档段落数目
  67.                 string [] test=new string[paraCount];                                              //新建一个字符串数组保存按照段落格式的doc文档
  68.                 string [] test1 = new string[paraCount];                                          //新建一个字符串数组保存文本内容
  69.                 for (int i = 0; i <paraCount; i++)
  70.                 {
  71.                     test[i] = doc.Paragraphs[i+1].Range.Text.Trim();                      //将段落写到字符串数组里面
  72.                 }
  73.                 foreach (string temp in test)
  74.                 {
  75.                     if (temp.Length==0 ||temp.EndsWith(":"))
  76.                     {
  77.                         //不做任何操作
  78.                     }
  79.                     else
  80.                     {
  81.                          //所做操作
  82.                     }
  83.                 }
  84.             }
  85.             catch (Exception ex)
  86.             {
  87.                 Console.WriteLine(ex.Message);
  88.                 Console.ReadLine();    
  89.             }
  90.         }
  91.         /// <summary>
  92.         /// 关闭文档
  93.         /// </summary>
  94.         public void closeFile()
  95.         {
  96.             try
  97.             {
  98.                 object unknow = Type.Missing;
  99.                 object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
  100.                 app.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
  101.             }
  102.             catch (Exception ex)
  103.             {
  104.                 Console.WriteLine(ex.Message);
  105.                 Console.ReadLine();
  106.             }
  107.         }
  108.         /// <summary>
  109.         /// 退出Word程序
  110.         /// </summary>
  111.         public void quit()
  112.         {
  113.             try
  114.             {
  115.                 object unknow = Type.Missing;
  116.                 object saveChanges = Word.WdSaveOptions.wdSaveChanges;
  117.                 app.Quit(ref saveChanges, ref unknow, ref unknow); 
  118.             }
  119.             catch (Exception ex)
  120.             {
  121.                 Console.WriteLine(ex.Message);
  122.                 Console.ReadLine();
  123.             }
  124.         }
  125.     }
  126. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值