Eli*_*sie 35 c# text-extraction ms-office
我试图使用C#从MS Word(.doc,.docx),Excel和Powerpoint中提取文本(字符串).我在哪里可以找到一个免费且简单的.Net库来阅读MS Office文档?我尝试使用NPOI,但我没有得到关于如何使用NPOI的样本.
Kyl*_*leM 37
对于Microsoft Word 2007和Microsoft Word 2010(.docx)文件,您可以使用Open XML SDK.这段代码将打开一个文档并将其内容作为文本返回.对于任何试图使用正则表达式来解析Word文档内容的人来说,它尤其有用.要使用此解决方案,您需要引用DocumentFormat.OpenXml.dll,它是OpenXML SDK的一部分.
请参阅:http://msdn.microsoft.com/en-us/library/bb448854.aspx
public static string TextFromWord(SPFile file)
{
const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
StringBuilder textBuilder = new StringBuilder();
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(file.OpenBinaryStream(), 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);
}
}
return textBuilder.ToString();
}
Run Code Online (Sandbox Code Playgroud)
adr*_*nks 26
使用PInvokes可以使用IFilter接口(在Windows上).许多常见文件类型的IFilter都随Windows一起安装(您可以使用此工具浏览它们.您可以要求IFilter从文件中返回文本.有几组示例代码(这是一个这样的示例).
Sep*_*Sep 15
Tika非常有用且易于从不同类型的文档中提取文本,包括Microsoft Office文件.
你可以使用这个项目,这是Kevin Miller制作的一件很好的艺术品 http://kevm.github.io/tikaondotnet/
只需添加此NuGet包 https://www.nuget.org/packages/TikaOnDotNet/
然后,这一行代码将完成魔术:
var text = new TikaOnDotNet.TextExtractor().Extract("fileName.docx / pdf / .... ").Text;
Run Code Online (Sandbox Code Playgroud)
让我稍微纠正KyleM给出的答案.我刚刚添加了两个额外节点的处理,这会影响结果:一个用"\ t"负责水平制表,另一个负责用于"\ v"的垂直制表.这是代码:
public static string ReadAllTextFromDocx(FileInfo fileInfo)
{
StringBuilder stringBuilder;
using(WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(dataSourceFileInfo.FullName, false))
{
NameTable nameTable = new NameTable();
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(nameTable);
xmlNamespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
string wordprocessingDocumentText;
using(StreamReader streamReader = new StreamReader(wordprocessingDocument.MainDocumentPart.GetStream()))
{
wordprocessingDocumentText = streamReader.ReadToEnd();
}
stringBuilder = new StringBuilder(wordprocessingDocumentText.Length);
XmlDocument xmlDocument = new XmlDocument(nameTable);
xmlDocument.LoadXml(wordprocessingDocumentText);
XmlNodeList paragraphNodes = xmlDocument.SelectNodes("//w:p", xmlNamespaceManager);
foreach(XmlNode paragraphNode in paragraphNodes)
{
XmlNodeList textNodes = paragraphNode.SelectNodes(".//w:t | .//w:tab | .//w:br", xmlNamespaceManager);
foreach(XmlNode textNode in textNodes)
{
switch(textNode.Name)
{
case "w:t":
stringBuilder.Append(textNode.InnerText);
break;
case "w:tab":
stringBuilder.Append("\t");
break;
case "w:br":
stringBuilder.Append("\v");
break;
}
}
stringBuilder.Append(Environment.NewLine);
}
}
return stringBuilder.ToString();
}
Run Code Online (Sandbox Code Playgroud)
聚会有点晚了,但是 - 现在你不需要下载任何东西 - 所有东西都已经安装了 .NET :(只需确保添加对 System.IO.Compression 和 System.IO.Compression.FileSystem 的引用)
using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;
using System.Text;
using System.IO.Compression;
public static class DocxTextExtractor
{
public static string Extract(string filename)
{
XmlNamespaceManager NsMgr = new XmlNamespaceManager(new NameTable());
NsMgr.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
using (var archive = ZipFile.OpenRead(filename))
{
return XDocument
.Load(archive.GetEntry(@"word/document.xml").Open())
.XPathSelectElements("//w:p", NsMgr)
.Aggregate(new StringBuilder(), (sb, p) => p
.XPathSelectElements(".//w:t|.//w:tab|.//w:br", NsMgr)
.Select(e => { switch (e.Name.LocalName) { case "br": return "\v"; case "tab": return "\t"; } return e.Value; })
.Aggregate(sb, (sb1, v) => sb1.Append(v)))
.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用Microsoft Office Interop。它是免费的和光滑的。在这里,我是如何从文档中提取所有单词的。
using Microsoft.Office.Interop.Word;
//Create Doc
string docPath = @"C:\docLocation.doc";
Application app = new Application();
Document doc = app.Documents.Open(docPath);
//Get all words
string allWords = doc.Content.Text;
doc.Close();
app.Quit();
Run Code Online (Sandbox Code Playgroud)
然后用这些词做任何您想做的。