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();
    
                }
            }
  • 相关阅读:
    Synplify9.6.2破解(转帖)
    让博客园博客自动生成章节目录索引
    如何学好FPGA
    verilog 不可综合语句
    在FPGA中使用for循环一定浪费资源吗?
    在verilog中调用VHDL模块
    C#和Java中执行SQL文件脚本的代码(非常有用)
    C#通用JSON帮助类
    公共的Json操作C#类
    Calendar.NET
  • 原文地址:https://www.cnblogs.com/jasonlny/p/3066423.html
Copyright © 2011-2022 走看看