zoukankan      html  css  js  c++  java
  • 兼容型Word 并带传统读法

     #region 传统读法

    object readOnly = true;
    object missing = System.Reflection.Missing.Value;
    object fileName = file.FullName;

    Microsoft.Office.Interop.Word.ApplicationClass wordapp
    = new ApplicationClass();

    //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用missing就行了)
    Microsoft.Office.Interop.Word.Document doc = wordapp.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 missing, ref missing, ref missing,
    ref missing, ref missing);
    //取得doc文件中的文本
    string text = doc.Content.Text;
    //关闭文件
    doc.Close(ref missing, ref missing, ref missing);
    //关闭COM
    wordapp.Quit(ref missing, ref missing, ref missing);


    #endregion


    //程序里静态的引用了Word,运行时如果没有安装是会报错的。像这样的程序要求一定要安装相应版本的程序。
    //可以考虑使用动态引用,这样可以通过代码来检测是否安装了程序,也可以做到版本兼容。
    #region 反射读


    Type objWordType
    = Type.GetTypeFromProgID("Microsoft.Office.Interop.Word");

    object objWordApp = Activator.CreateInstance(objWordType);
    object objDocuments = objWordApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, objWordApp, null);
    object[] openParas = { file.FullName };
    object openDocument = objDocuments.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, objDocuments, openParas);


    object openContent = openDocument.GetType().InvokeMember("Content", BindingFlags.GetProperty, null, openDocument, null);

    object opentText = openContent.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, openContent, null);



    //设置界面是否 显示 。 读得话 不显示了
    //object[] pars = { true };
    // objWordApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, objWordApp, pars);


    #endregion


    return opentText.ToString();

  • 相关阅读:
    函数指针和回调函数
    MATLAB神经网络(2) BP神经网络的非线性系统建模——非线性函数拟合
    MATLAB神经网络(1)之R练习
    R时间序列分析实例
    自动控制理论的MATLAB仿真实例(二)
    自动控制理论的MATLAB仿真实例(一)
    Mathtype快捷键&小技巧
    Latex数学符号对应表
    MATLAB神经网络(1) BP神经网络的数据分类——语音特征信号分类
    R语言实战(三) 图形初阶
  • 原文地址:https://www.cnblogs.com/shikyoh/p/2182304.html
Copyright © 2011-2022 走看看