Hi Team,
We need to read all the recursive table data inside a table (Nth level if possible) and get the cell values for all of them. Please let us know if there is any quick solution available for this.
Many thanks in advance!!
string previousContent;
void Main()
{
Document doc = new Document("test.docx");
Body body = doc.Sections[0].Body;
FileStream fileStream = File.Create("result.txt");
fileStream.Close();
foreach (ITable table in body.Tables)
{
foreach(TableRow tableRow in table.Rows)
{
foreach(TableCell tableCell in tableRow.Cells)
{
foreach(DocumentObject documentObject in tableCell.ChildObjects)
{
if(documentObject.DocumentObjectType == DocumentObjectType.Paragraph)
{
Paragraph cellParagh = (Paragraph)documentObject;
previousContent = File.ReadAllText("result.txt");
File.WriteAllText("result.txt", previousContent+cellParagh.Text+";");
}
if (documentObject.DocumentObjectType == DocumentObjectType.Table)
{
Table cellTable = (Table)documentObject;
GetTableContent(cellTable);
}
}
}
}
}
}
private void GetTableContent(Table table)
{
StringBuilder sb = new StringBuilder();
foreach (TableRow tableRow in table.Rows)
{
foreach (TableCell tableCell in tableRow.Cells)
{
foreach (DocumentObject documentObject in tableCell.ChildObjects)
{
if (documentObject.DocumentObjectType == DocumentObjectType.Paragraph)
{
Paragraph cellParagh = (Paragraph)documentObject;
previousContent = File.ReadAllText("result.txt");
File.WriteAllText("result.txt", previousContent + cellParagh.Text+";");
}
if (documentObject.DocumentObjectType == DocumentObjectType.Table)
{
Table cellTable = (Table)documentObject;
GetTableContent(cellTable);
}
}
}
}
}