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();
    
                }
            }
  • 相关阅读:
    greenDao 学习之坑 "java-gen" 目录下的类不能引用
    fastboot 刷system.img 提示 sending 'system' (*KB)... FAILED (remote: data too large)
    AndroidStudio导入第三方开源库 --文件夹源码
    git 克隆项目 与 分支简单操作
    Jquery当选中后费用或什么信息会自增长
    Jquery中各种标签的含义集合
    Jquery判断是否重复(连接到后台数据库进行比较)
    Jquery按空格键选中复选框或单选框
    JS(JQuery)操作Array的相关方法
    前端知识点-人资相关知识点
  • 原文地址:https://www.cnblogs.com/jasonlny/p/3066423.html
Copyright © 2011-2022 走看看