zoukankan      html  css  js  c++  java
  • 提取Word里的文本内容 C#

         using DocumentFormat.OpenXml.Packaging;  
         public
    static string TextFromWord(string path) { const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; StringBuilder textBuilder = new StringBuilder(); using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(path, false)) { // Manage namespaces to perform XPath queries. NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("w", wordmlNamespace); // Get the document part from the package. // Load the XML in the document part into an XmlDocument instance. XmlDocument xdoc = new XmlDocument(nt); xdoc.Load(wdDoc.MainDocumentPart.GetStream()); XmlNodeList paragraphNodes = xdoc.SelectNodes("//w:p", nsManager); foreach (XmlNode paragraphNode in paragraphNodes) { XmlNodeList textNodes = paragraphNode.SelectNodes(".//w:t", nsManager); foreach (System.Xml.XmlNode textNode in textNodes) { textBuilder.Append(textNode.InnerText); } textBuilder.Append(Environment.NewLine); } } var result = textBuilder.ToString(); return result; }

    异常情况:抛出异常:文件包含损坏的数据

    解决办法:把doc文件转化为docx文件,可搜索在线转化网站。之后就可以使用docx文件。

  • 相关阅读:
    Validator 字段验证
    DRF实现发送短信验证码接口
    云片网发送短信验证码
    DRF自定义用户认证
    php服务器端与android客户端通信问题
    转 java调用php的webService
    Ubuntu12.04安装vim7.3
    ubuntu12.10更新源
    源列表
    Ubuntu 12.10 用wubi安装到硬盘中
  • 原文地址:https://www.cnblogs.com/dayang12525/p/8675269.html
Copyright © 2011-2022 走看看