+ -
当前位置:首页 → 问答吧 → 怎么把Word中的数据写到C#的一个对象中去啊

怎么把Word中的数据写到C#的一个对象中去啊

时间:2011-12-16

来源:互联网

rt

作者: youlehong   发布时间: 2011-12-16

给你一个类
C# code

using Microsoft.Office.Interop.Word;
class opertateWord
    {
        public string contentText = "";
        public void WordToPdf(string sourcePath)
        {
            object paramMissing = Type.Missing;
            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word._Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                wordDocument = wordApplication.Documents.Open(
                    ref paramSourceDocPath,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing,
                    ref paramMissing);
                string text;
                if (wordDocument != null)
                {
                    text = wordDocument.Content.Text;//这里只取word文本,要取其它内容也可以在这里选择。
                    contentText = text;
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("错误提示:" + e.Message);
            }

            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }

作者: itol925   发布时间: 2011-12-16

顶,不过一楼不够详细,
 #region 读取

  /// <summary>
  /// 读取一段
  /// </summary>
  /// <param name="ParaCount">/第几段</param>
  /// <param name="ReadOnly">/是否只读</param>
  /// <returns></returns>
  public string Read(int ParaCount, bool ReadOnly)
  {

  string txt = Convert.ToString(MyDoc.Paragraphs[ParaCount].Range.Text);
  return txt;

  }
  /// <summary>
  /// 读取指定长度
  /// </summary>
  /// <param name="Start">/开始位置</param>
  /// <param name="End">/结束位置</param>
  /// <returns></returns>
  public string Read(int Start, int End)
  {

  object start = Start;
  object end = End;
  string txt = "";
  txt = Convert.ToString(MyDoc.Range(ref start, ref end).Text);
  return txt;
  }
  #endregion
  #region 表相关操作
  /// <summary>
  /// 读取表的位置
  /// </summary>
  /// <param name="TableIndex">/哪个表</param>
  public Word.Range TableLocation(int TableIndex)
  {

  MyRange = MyDoc.Tables[TableIndex].Range;
  return MyRange;
  }
  /// <summary>
  /// 当前文档内有几个表
  /// </summary>
  /// <returns></returns>
  public int TableCount()
  {
  return MyDoc.Tables.Count;
  }
  /// <summary>
  /// 返回一个表的内容
  /// </summary>
  /// <param name="TableIndex">/哪个表</param>
  /// <returns></returns>
  public Word.Table Read(int TableIndex)
  {
  return MyDoc.Tables[TableIndex];
  }
  #endregion

作者: woairuijuan9   发布时间: 2011-12-16

学习。。。

作者: hefeng_aspnet   发布时间: 2011-12-16

我的方法加到一楼的类里绝对够用了,散分散分.......

作者: woairuijuan9   发布时间: 2011-12-16