I was trying to extract a text(string) from MS Word (.doc, .docx), Excel and Powerpoint using C#. Where can i find a free and simple .Net library to read MS Office documents? I tried to use NPOI but i didn't get a sample about how to use NPOI.
I was trying to extract a text(string) from MS Word (.doc, .docx), Excel and Powerpoint using C#. Where can i find a free and simple .Net library to read MS Office documents? I tried to use NPOI but i didn't get a sample about how to use NPOI.
Using PInvokes you can use the IFilter interface (on Windows). The IFilters for many common file types are installed with Windows (you can browse them using this tool. You can just ask the IFilter to return you the text from the file. There are several sets of example code (here is one such example).
Use The Microsoft Office Interop. It's free and slick. Here how I pulled all the words from a doc.
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();
Then do whatever you want with the words.
Simple!
These two steps will get you there:
1) Use the Office Interop library to convert DOC to DOCX
2) Use DOCX2TXT to extract the text from the new DOCX
The link for 1) has a very good explanation of how to do the conversion and even a code sample.
An alternative to 2) is to just unzip the DOCX file in C# and scan for the files you need. You can read about the structure of the ZIP file here.
Edit: Ah yes, I forgot to point out as Skurmedel did below that you must have Office installed on the system on which you want to do the conversion.
I did a docx text extractor once, and it was very simple. Basically docx, and the other (new) formats I presume, is a zip-file with a bunch of XML-files instead. The text can be extracted using a XmlReader and using only .NET-classes.
I don't have the code anymore, it seems :(, but I found a guy who have a similar solution.
Maybe this isn't viable for you if you need to read .doc and .xls files though, since they are binary formats and probably much harder to parse.
There is also the OpenXML SDK, still in CTP though, released by Microsoft.
One of the suitable options for extracting text from Office documents in C# is GroupDocs.Parser for .NET API. The following are the code samples for extracting simple as well as formatted text.
Extracting Text
// Create an instance of Parser class
using(Parser parser = new Parser("sample.docx"))
{
// Extract a text into the reader
using(TextReader reader = parser.GetText())
{
// Print a text from the document
// If text extraction isn't supported, a reader is null
Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd());
}
}
Extracting Formatted Text
// Create an instance of Parser class
using (Parser parser = new Parser("sample.docx"))
{
// Extract a formatted text into the reader
using (TextReader reader = parser.GetFormattedText(new FormattedTextOptions(FormattedTextMode.Html)))
{
// Print a formatted text from the document
// If formatted text extraction isn't supported, a reader is null
Console.WriteLine(reader == null ? "Formatted text extraction isn't suppported" : reader.ReadToEnd());
}
}
Disclosure: I work as Developer Evangelist at GroupDocs.