DocxToTextDemo

文件结构

+DocxToTextDemo

    +bin

        +Debug

            -DocxToTextDemo.vshost.exe

        -ICSharpCode.SharpZipLib.dll

        +Release

            -DocxToTextDemo.exe

            -ICSharpCode.SharpZipLib.dll

    -DocxToText.cs

    -DocxToTextDemo.csproj

    -Form1.cs

    -Form1.Designer.cs

    -Form1.resx

    +obj

        +Debug

            +TempPE

    -Program.cs

    +Properties

        -AssemblyInfo.cs

        -Resources.Designer.cs

        -Resources.resx

        -Settings.Designer.cs

        -Settings.settings

-DocxToTextDemo.sln

-DocxToTextDemo.suo

说明

通过本程序可以直接抽取docx后缀名的word文件,生成txt内容              小S资料屋

 

// DocxToText.cs

// Copyright (C) 2007  Eugene Pankov

//

 

using System;

using System.IO;

using System.Text;

using System.Xml;

using ICSharpCode.SharpZipLib.Zip;

 

 

namespace DocxToTextDemo

{

    public class DocxToText

    {

        private const string ContentTypeNamespace =

            @"http://schemas.openxmlformats.org/package/2006/content-types";

 

        private const string WordprocessingMlNamespace =

            @"http://schemas.openxmlformats.org/wordprocessingml/2006/main";

 

        private const string DocumentXmlXPath =

            "/t:Types/t:Override[@ContentType=\"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml\"]";

 

        private const string BodyXPath = "/w:document/w:body";

 

        private string docxFile = "";

        private string docxFileLocation = "";

 

 

        public DocxToText(string fileName)

        {

            docxFile = fileName;

        }

 

        #region ExtractText()

        /// <summary>

        /// Extracts text from the Docx file.

        /// </summary>

        /// <returns>Extracted text.</returns>

        public string ExtractText()

        {

            if (string.IsNullOrEmpty(docxFile))

                throw new Exception("Input file not specified.");小S资料屋

 

            // Usually it is "/word/document.xml"

 

            docxFileLocation = FindDocumentXmlLocation();

 

            if (string.IsNullOrEmpty(docxFileLocation))

                throw new Exception("It is not a valid Docx file.");

 

            return ReadDocumentXml();

        }

        #endregion

 

        #region FindDocumentXmlLocation()

        /// <summary>

        /// Gets location of the "document.xml" zip entry.

        /// </summary>

        /// <returns>Location of the "document.xml".</returns>

        private string FindDocumentXmlLocation()

        {

            ZipFile zip = new ZipFile(docxFile);

            foreach (ZipEntry entry in zip)

            {

                // Find "[Content_Types].xml" zip entry

 

                if (string.Compare(entry.Name, "[Content_Types].xml", true) == 0)

                {

                    Stream contentTypes = zip.GetInputStream(entry);

 

                    XmlDocument xmlDoc = new XmlDocument();

                    xmlDoc.PreserveWhitespace = true;

                    xmlDoc.Load(contentTypes);

                    contentTypes.Close();

 

                    //Create an XmlNamespaceManager for resolving namespaces小S资料屋

 

                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

                    nsmgr.AddNamespace("t", ContentTypeNamespace);

 

                    // Find location of "document.xml"

 

                    XmlNode node = xmlDoc.DocumentElement.SelectSingleNode(DocumentXmlXPath, nsmgr);

 

                    if (node != null)

                    {

                        string location = ((XmlElement) node).GetAttribute("PartName");

                        return location.TrimStart(new char[] {'/'});

                    }

                    break;

                }

            }

            zip.Close();

            return null;

        }

        #endregion

 

        #region ReadDocumentXml()

        /// <summary>

        /// Reads "document.xml" zip entry.

        /// </summary>

        /// <returns>Text containing in the document.</returns>

        private string ReadDocumentXml()

        {

            StringBuilder sb = new StringBuilder();

 

            ZipFile zip = new ZipFile(docxFile);

            foreach (ZipEntry entry in zip)

            {

                if (string.Compare(entry.Name, docxFileLocation, true) == 0)

                {

                    Stream documentXml = zip.GetInputStream(entry);

 

                    XmlDocument xmlDoc = new XmlDocument();

                    xmlDoc.PreserveWhitespace = true;

                    xmlDoc.Load(documentXml);

                    documentXml.Close();

 

                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

                    nsmgr.AddNamespace("w", WordprocessingMlNamespace);

 

                    XmlNode node = xmlDoc.DocumentElement.SelectSingleNode(BodyXPath, nsmgr);

 

                    if (node == null)

                        return string.Empty;

 

                    sb.Append(ReadNode(node));

 

                    break;

                }

            }

            zip.Close();

            return sb.ToString();

        }

        #endregion小S资料屋

 

        #region ReadNode()

        /// <summary>

        /// Reads content of the node and its nested childs.

        /// </summary>

        /// <param name="node">XmlNode.</param>

        /// <returns>Text containing in the node.</returns>

        private string ReadNode(XmlNode node)

        {

            if (node == null || node.NodeType != XmlNodeType.Element)

                return string.Empty;

 

            StringBuilder sb = new StringBuilder();

            foreach (XmlNode child in node.ChildNodes)

            {

                if (child.NodeType != XmlNodeType.Element) continue;

 

                switch (child.LocalName)

                {

                    case "t":                           // Text

                        sb.Append(child.InnerText.TrimEnd());

 

                        string space = ((XmlElement)child).GetAttribute("xml:space");

                        if (!string.IsNullOrEmpty(space) && space == "preserve")

                            sb.Append(' ');

 

                        break;

 

                    case "cr":                          // Carriage return

                    case "br":                          // Page break

                        sb.Append(Environment.NewLine);

                        break;

 

                    case "tab":                         // Tab

                        sb.Append("\t");

                        break;

 

                    case "p":                           // Paragraph

                        sb.Append(ReadNode(child));

                        sb.Append(Environment.NewLine);

                        sb.Append(Environment.NewLine);

                        break;

 

                    default:

                        sb.Append(ReadNode(child));

                        break;

                }

            }

            return sb.ToString();

        }

        #endregion

    }

}

相关资料

 

本文地址:http://www.dfwlt.com/forum.php?mod=viewthread&tid=46&extra=page%3D1,转发请保留这个地址,谢谢

转载于:https://www.cnblogs.com/xszlo/archive/2013/02/23/2923350.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值