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.

Wed Nov 09, 2022 7:46 pm

I have a table in a word document and I want to copy the header style so that the new rows are inserted with that style
You want to keep the style of the first row. You can set the first style and save the style, then apply to the next row

for (int f = 0; f < dataCuenta.Length; f++)
{

dataRow = tableCuenta.AddRow();

for (int c = 0; c < HeaderCuenta.Length; c++)
{
dataRow.Cells[c].AddParagraph().AppendText(dataCuenta[f][c].Trim());
}
}

emiliorojas
 
Posts: 1
Joined: Wed Aug 24, 2022 1:27 pm

Thu Nov 10, 2022 9:56 am

Hello,

Yes, you can save the style of the first row and apply it to the other row. I attached the sample code for your reference.
If you have any issue, just feel free to contact us.
Code: Select all
  //Open a blank Word document as template
            Document document = new Document();
            document.LoadFromFile(@"../../data/input.docx");

            Section section = document.Sections[0];

            Table table = section.Tables[0] as Table;

            //Get the format of the first
            float firstHeight = table.Rows[0].Height;
            TableRowHeightType firstHeightType = table.Rows[0].HeightType;
            float firstCellSpace = table.Rows[0].RowFormat.CellSpacing;
           

            //Insert new row
            table.AddRow();

            //Set the style of first row to new row
            table.Rows[2].RowFormat.CellSpacing = firstCellSpace;
            table.Rows[2].HeightType = firstHeightType;
            table.Rows[2].Height = firstHeight;

            //Insert text for new row
            table.Rows[2].Cells[0].AddParagraph().AppendText("1");
            table.Rows[2].Cells[1].AddParagraph().AppendText("2");
            table.Rows[2].Cells[2].AddParagraph().AppendText("3");
            table.Rows[2].Cells[3].AddParagraph().AppendText("4");
            table.Rows[2].Cells[4].AddParagraph().AppendText("5");

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


Sincerely
Abel
E-iceblue support team
User avatar

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

Return to Spire.Doc