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();
    
                }
            }
  • 相关阅读:
    SQL SERVER 导出到Oracle 问题与技巧
    oracle Array类型作为参数传入函数(存储过程) 大字符串参数解决方案
    Oracle 时间处理(加减)
    批处理(命令行)安装数据库
    导致Asp.Net站点重启的10个原因 ,记录重启原因
    关于vs2010 起始页
    SQL Server 导数据 Oracle
    MYSQL正确删除binlog的方法
    yum 安装supervisor
    redis集群配置与管理
  • 原文地址:https://www.cnblogs.com/jasonlny/p/3066423.html
Copyright © 2011-2022 走看看