Extract tables from Microsoft Office Word documents
Extract tables from Microsoft Office Word documents
Leave feedback
To extract tables from Microsoft Office Word document GetStructure method is used. This method returns XML representation of the document. Tables are represented by “table” tag. For more details, see Extract text structure.
Warning
GetStructure method returns null value if text structure extraction isn’t supported for the document. For example, text structure extraction isn’t supported for TXT files. Therefore, for TXT file GetStructure method returns null. If Microsoft Office Word document has no text, GetStructuremethod returns an empty XmlReader object.
Here are the steps to extract tables from Microsoft Office Word documents:
Instantiate Parser object for the initial document;
The following example demonstrates how to extract tables from Microsoft Office Word document:
// Create an instance of Parser classusing(Parserparser=newParser(filePath)){// Get the reader object for the document XML representationusing(XmlReaderreader=parser.GetStructure()){// Iterate over the documentwhile(reader.Read()){// Check if this is the start of the tableif(reader.IsStartElement()&&reader.Name=="table"){// Process the tableProcessTable(reader);}}}}privatestaticvoidProcessTable(XmlReaderreader){Console.WriteLine("table");// Create an instance of StringBuilder to store the cell valueStringBuildervalue=newStringBuilder();// Iterate over the tablewhile(reader.Read()){// Check if the current tag is the end of the tableboolisTableEnd=!reader.IsStartElement()&&reader.Name=="table";// Check if the current tag is the start of the row or the cellboolisRowOrCellStart=reader.IsStartElement()&&(reader.Name=="tr"||reader.Name=="td");// Print the cell value if this is the end of the table or the start of the row or the cellif((isTableEnd||isRowOrCellStart)&&value.Length>0){Console.Write(" ");Console.WriteLine(value.ToString());value=newStringBuilder();}// If this is the end of the table - return to the main functionif(isTableEnd){return;}// If this is the start of the row or the cell - print the tag nameif(isRowOrCellStart){Console.WriteLine(reader.Name);continue;}// If this code line is reached then this is the value of the cellvalue.Append(reader.Value);}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.