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 Jun 24, 2021 1:58 pm

Dear support team,

I'm quite new to your framework and I wonder how to make several copies of existing rows of a table. The sample table consists of 3 rows - 1st is header row, 2nd is data row and 3rd is also a data row containing a nested table.

How can I make an exact copy of all rows except the header row of the outer table?
The rows and cells copied need to preserve their formatting.

Please find attached a template to make the requirement clear.

I really appreciate your feedback.
Thank you very much.

Kind regards,
Marcus Gattinger

mgattinger
 
Posts: 31
Joined: Fri May 21, 2021 9:03 am

Fri Jun 25, 2021 3:51 am

Hello Marcus,

Thanks for your inquiry.
Please refer to the following code to meet your requirement.
Code: Select all
            string input = @"Template.docx";
            Document doc = new Document();
            doc.LoadFromFile(input);
            Section se = doc.Sections[0];
            Table table = (Table)se.Tables[0];

            Table newTable = se.AddTable();
            newTable.Rows.Add(table.FirstRow.Clone());
            for (int num=0; num < 3; num++)
            {
                for (int i = 1; i < table.Rows.Count; i++)
                {
                    newTable.Rows.Add(table.Rows[i].Clone());
                }
            }

            string output = "Result.docx";
            doc.SaveToFile(output, FileFormat.Docx);


If there are any other questions related to our products, please do not hesitate to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

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

Fri Jun 25, 2021 7:54 am

Thank you very much for your quick response. Exactly what I was asking about.

mgattinger
 
Posts: 31
Joined: Fri May 21, 2021 9:03 am

Fri Jun 25, 2021 8:09 am

You are welcome. And I am glad that the code I provided can help you.
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