Free Support Forum - aspose.com

Reading a table in Word document

How do I read a table on the word document. Can you please send me snippet of code. I looked through the help and could not find anything.

Thanks

Hi,

Sorry I'm not quite sure what you mean by "read a table". Please provide more details of your task.

I have a word document that contains a table with data. I would like to read all the data in the table.

Thanks

Okay. Tables in Word documents are represented by the Table objects in the Aspose.Words object model. You can easily retrieve them from the document. Imagine you need to access the first table of the first section's body:

Table table = doc.FirstSection.Body.Tables[0];

Now that you have the Table object, you can access its rows and cells. For example, to read the text contained in the first cell of the first row, implement the following line of code:

string text = table.Rows[0].Cells[0].Range.Text;

To read the text of the entire table as a whole, use the following line:

string text = table.Range.Text;

Please let me know if something is still not clear to you.

I'm using Aspose.Words for .net and I can not get an individual cells text using the methods above.

My code is as follows:

For Each table In wdDoc.LastSection.Body.Tables

If table.FirstRow.FirstCell.ToTxt.Trim = "Field Name" Then

If table.FirstRow.LastCell.ToTxt.Trim = "Description" Then

For i = 1 To table.Rows.Count - 1

term = table.Rows[i].Cells[0].Range.Text

Next

End If

End If

Next

I'm using MS Visual Studio, and when I type table.Rows[i]. Cells does not come up as an available method. Range does not have a Text method either, only a ToTxt method.

I found a way to use the API and grab the text from the first and last cells of the first row, but I need to get the text from the rest of the table a cell at a time also.

Oh, I'm using Aspose.Words version 4.2.4

Thanks,

Allison

Hi Allison,

Square brackets are used to denote indexer in C#. In VB .NET, you should use parentheses instead:

term = table.Rows(i).Cells(0).Range.Text

Thanks.

Thank you. Now that I have read the cell contents, can I change the contents? The Range.Text method is read only, so I can't just set easily. What methods are available for this?

Thanks,

Allison

Sure. To add a text or other nodes to a cell, use the DocumentBuilder class:

Dim cell As Cell = table.Rows(i).Cells(0)

cell.FirstParagraph.ChildNodes.Clear()

builder.MoveTo(cell.FirstParagraph)

builder.Write("Hello")

DocumentBuilder also has the MoveToCell method that allows to directly move to a table cell:

http://www.aspose.com/Products/Aspose.Words/Api/Aspose.Words.DocumentBuilder.MoveToCell.html

Thanks.

hallo i would like to quote one question

may i know Table table = doc.FirstSection.Body.Tables[0]; this construct

as document.getrange().getsection., but there is no method like Body , Kindly requested to revert back with the requested information.

Thanking you
Charles v c

charlesvc:
hallo i would like to quote one question

may i know Table table = doc.FirstSection.Body.Tables[0]; this construct

as document.getrange().getsection., but there is no method like Body , Kindly requested to revert back with the requested information.

Thanking you
Charles v c

Hi

Thanks for your inquiry. Please try using this code snippet.

doc.getFirstSection().getBody().getTables().get(0)

Hope this helps.

Best regards.