zoukankan      html  css  js  c++  java
  • C#操作Word需要注意的一些问题

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

    一、首先添加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();
    
                }
            }
  • 相关阅读:
    DShow实现一个avi视频的播放(含有个人解释和注释)
    指针和引用的区别
    从头看算法导论 习题2.3-7 深入分析
    程序员福利各大平台免费接口,非常适用
    NYOJ 58 最少步数
    NYOJ 42 一笔画问题
    NYOJ 1058 部分和问题
    NYOJ 21 三个水杯
    winform里面网页显示指定内容
    C#中List<T>对象的深度拷贝问题
  • 原文地址:https://www.cnblogs.com/jasonlny/p/3066423.html
Copyright © 2011-2022 走看看