Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Apr 29, 2021 11:16 am

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!!

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Fri Apr 30, 2021 5:54 am

Hello,

Thanks for your inquiry.
Please refer to the following modified code to meet your needs. If there are still any issues, please provide us with your input file and your desired output for reference. Thanks in advance.
Code: Select all
        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);
                        }
                    }
                }
            }   
        }



Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Apr 30, 2021 7:50 am

Thanks Brian for your response.

Unfortunately, it didn't give me the expected result for one of my sample doc.

Please find the attached zip to get the sample doc for reference.

Thanks.

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Fri Apr 30, 2021 9:55 am

Hello,

Thanks for providing your input file.
I tested your case with the latest Spire.Doc Pack(hot fix) Version:9.4.18, but I found that the table contents can be extracted correctly.

What’s your expected result? To help us better understand your requirement, please describe it in detail, or provide a .txt file with your expected output. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Sat May 08, 2021 7:21 am

Hello,

Has your issue been resolved? Any feedback will be greatly appreciated.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon May 10, 2021 5:17 am

Thanks Brian.

Yes, given solution worked after little tweaks since we needed the tables in HTML form.

Much appreciated for your help.

pr20080798
 
Posts: 146
Joined: Wed Jan 20, 2021 1:15 pm

Mon May 10, 2021 6:36 am

You are welcome.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc