没有Word安装的通用Microsoft Word文档解析器

时间:2014-02-18 08:50:19

标签: c# wpf ms-word

为了解析:Microsoft Word 97/2003(.doc)& Microsoft Word 2007/2010(.docx)使用C#和WPF而不安装Word,我需要知道是否有人可以给我一个严肃的库来实现它。

从技术上讲,我会像这样迭代ZipEntry元素:

foreach (string file in _listPathFiles)
        {
            using (Ionic.Zip.ZipFile zip = ZipFile.Read(file))
            {
                try
                {
                    zip.ToList().ForEach(entry =>
                    {
                        if (entry.FileName.EndsWith(".doc") ||
                            entry.FileName.EndsWith(".docx"))
                        {
                            // Extract file into disk
                            entry.FileName = System.IO.Path.GetFileName(entry.FileName);
                            entry.Extract(baseStoragePath);

                            // Get data from file with Parser
                            string filePath = baseStoragePath + entry.FileName;


                            // Remove extracted filess
                            if (File.Exists(filePath))
                            {
                                File.Delete(filePath);
                                Console.WriteLine("Delete : " + filePath);
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine("Fail to unzip Exception : " + e.StackTrace);
                }
            }
        }

我不确定我是否可以直接使用ZipEntry来获取文档,可能在解析之前我必须解压缩它?!

我的目标是将数据放在“标题1”Microsoft Word样式之后,因此库应该能够获得这种属性。

欢迎图书馆的想法和代码示例..

2 个答案:

答案 0 :(得分:1)

GroupDocs.Parser for .NET可用于从Word文档中提取文本而无需安装MS Word的情况。提取可以逐行或一次执行。

// extracting all the text 
WordsTextExtractor extractor = new WordsTextExtractor("sample.docx");
Console.Write(extractor.ExtractAll());

// OR

// Extract text line by line
string line = extractor.ExtractLine();

// If the line is null, then the end of the file is reached
while (line != null)
{
      // Print a line to the console
      Console.Write(line);
      // Extract another line
      line = extractor.ExtractLine();
}

披露:我是GroupDocs的开发人员布道者。

答案 1 :(得分:-1)

查看NPOI(Apache NOI API的.NET端口): http://npoi.codeplex.com/

下载OpenXML SDK以阅读MS Word等Office文档。

相关问题