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 Dec 08, 2022 10:06 pm

When we add a row to a table, the cells default to Normal style.
Is there a way to specify an alternative style to be used on rows and cells added to the table?

Les43625
 
Posts: 12
Joined: Tue Feb 22, 2022 1:41 am

Fri Dec 09, 2022 7:02 am

Hello,

Thanks for your inquiry.
For your requirement, you can refer to the following code. And I attached my test input word file and output word file for your reference.
If you have any issue, just feel free to contact us.

Code: Select all
// Load Word document from disk
            Document document = new Document();
            document.LoadFromFile(@"..\..\data\input.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Get tables
            Table tb1 = section.Tables[0] as Table;

            //Set style for row2
            TableRow row2 = tb1.Rows[1];

            //Set back color for row2
            row2.RowFormat.BackColor = Color.LightGray;
            row2.RowFormat.CellSpacing = 2;

            row2.HeightType = TableRowHeightType.Exactly;
            row2.Height = 20f;

            //Set style for row3
            TableRow row3 = tb1.Rows[2];

            //Set back color for cell1 in row3
            row3.Cells[0].CellFormat.BackColor = Color.Red;

            //Set width and color for border of cell2
            row3.Cells[1].CellFormat.Borders.BorderType = BorderStyle.Single;
            row3.Cells[1].CellFormat.Borders.LineWidth = 1f;
            row3.Cells[1].CellFormat.Borders.Left.Color = Color.Red;
            row3.Cells[1].CellFormat.Borders.Right.Color = Color.Red;
            row3.Cells[1].CellFormat.Borders.Top.Color = Color.Red;
            row3.Cells[1].CellFormat.Borders.Bottom.Color = Color.Red;

            //Save the result word file
            document.SaveToFile(@"../../output/output.docx",FileFormat.Docx);


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Jan 11, 2023 9:45 am

Hello,

Greeting from E-iceblue.
Does the code I provided meet yout requirement? Could you please give some feedback at your convenience.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc