zoukankan      html  css  js  c++  java
  • C#读取Word文档内容代码

    首先要添加引用com组件:

    然后引用:

    using Word = Microsoft.Office.Interop.Word;

    获取内容:

    ///
    /// 读取 word文档 返回内容
    ///
    
    //////
    public static string GetWordContent(string path)
    {
    try
    {
    Word.Application app = new Microsoft.Office.Interop.Word.Application();
    Type wordType = app.GetType();
    Word.Document doc = null;
    object unknow = Type.Missing;
    app.Visible = false;
    
    object file = path;
    doc = app.Documents.Open(ref file,
    ref unknow, ref unknow, ref unknow, ref unknow,
    ref unknow, ref unknow, ref unknow, ref unknow,
    ref unknow, ref unknow, ref unknow, ref unknow,
    ref unknow, ref unknow, ref unknow);
    int count = doc.Paragraphs.Count;
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= count; i++)
    {
    
    sb.Append(doc.Paragraphs[i].Range.Text.Trim());
    }
    
    doc.Close(ref unknow, ref unknow, ref unknow);
    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, app, null);
    doc = null;
    app = null;
    //垃圾回收
    GC.Collect();
    GC.WaitForPendingFinalizers();
    
    string temp=sb.ToString();
    //if (temp.Length > 200)
    // return temp.Substring(0, 200);
    //else
    return temp;
    }
    catch
    {
    return "";
    }
    }
  • 相关阅读:
    第一周作业
    第0次作业
    第三次作业
    第二次作业
    第一次作业
    第零次作业
    第三周作业
    第二周作业
    第一周作业
    关于如何学习计算机
  • 原文地址:https://www.cnblogs.com/huhangfei/p/5013033.html
Copyright © 2011-2022 走看看