linzy

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在网上找了好些资料,基本上是大同小异,我在操作的过程中遇到了一些问题,做下总结:

一、首先添加morcosoft word 12.0 object Library引用(低版本的com也可以)

using Word = Microsoft.Office.Interop.Word;

二、无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。

在引用里面,Microsoft.Office.Interop.Word-->选择属性-->嵌入互操作True类型改为False

三、未能加载文件或程序集“Interop.Microsoft.Office.Core, Version=2.4.0.0,。。。

如果你安装了office组件当仍然报这个错误但没有安装到GAC里面,一是因为你在安装office2003之前已经安装上了“.Net Framework”,二是因为你在安装office2003的时候选漏了“.Net 可编程性支持”,安装完成重新导入下。

四、附一个简单读取Word字符内容

/// <summary>
        /// 简单读取Word字符内容
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns>word中的字符内容(纯文本)</returns>
        public static string ReadStringFromWord(string FilePath)
        {
            Word.ApplicationClass app = null;
            Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object FileName = FilePath;
            object readOnly = true;
            object isVisible = false;


            try
            {
                app = new Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);

                string textString = "";

                //读取全部内容
                textString = doc.Content.Text.Trim();

                textString = textString.Replace("\v", "\n");
                return textString;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (doc != null)
                {
                    try
                    {
                        doc.Close(ref missing, ref missing, ref missing);
                    }
                    catch
                    { }
                    doc = null;
                }
                if (app != null)
                {
                    try
                    {
                        app.Quit(ref missing, ref missing, ref missing);
                    }
                    catch
                    { }
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();

            }
        }

 

posted on 2013-05-08 11:35  linzy  阅读(633)  评论(0编辑  收藏  举报