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 Apr 29, 2020 3:53 am

Hi,

I can't find an instruction on how to add a column to a table.

I attach my word file just in case you want to make an example.
sample to iceblue.zip


Thank you,
Quan

MicroGenDX
 
Posts: 2
Joined: Fri Mar 22, 2019 9:46 pm

Wed Apr 29, 2020 7:08 am

Hello,

Thanks for your inquiry.
The following code demonstrates how to add a new column to an existing table. If you have further questions, please feel free to write back.
Code: Select all
    static void Main(string[] args)
    {
        Document doc = new Document();
        doc.LoadFromFile("sample to iceblue.docx");
        Section section = doc.Sections[0];
        Table table = section.Tables[0] as Table;

        //Set the column index
        int columnIndex = 4;       
        if (columnIndex <= table.Rows[0].Cells.Count)
        {
            //Add a new column
            AddColumn(table, columnIndex);
        }
        else
        {
            Console.WriteLine("Index is out of range");
        }           
        string output = "out.docx";
        doc.SaveToFile(output, FileFormat.Docx2013);
    }

    public static void AddColumn(Table table, int columnIndex)
    {
        //Get the total grid span
        int gridSpan = 0;
        for (int i = 0; i < columnIndex; i++)
        {
                gridSpan += table.Rows[0].Cells[i].GridSpan;
        }           
        for (int r = 0; r < table.Rows.Count; r++)
        {               
            //Add a new cell
            TableCell addCell = new TableCell(table.Document);
            int currentGridSpan = 0;
            for (int i = 0; i < table.Rows[r].Cells.Count; i++)
            {
                //Calculate the current total grid span
                currentGridSpan += table.Rows[r].Cells[i].GridSpan;
                if (currentGridSpan == gridSpan)
                {
                    table.Rows[r].Cells.Insert(i + 1, addCell);
                    break;
                }
                if (currentGridSpan > gridSpan)
                {
                    table.Rows[r].Cells.Insert(i, addCell);
                    break;
                }
            }                             
        }
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Jun 02, 2020 8:56 am

Hello,

Hope you are doing well.
Has your issue been resolved? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Jul 02, 2021 10:13 am

Hello , I have followed this and its working perfectly but there is another problem i am facing regarding adding a column that is i couldn't be able to add header of that new column and few data also for that column , could you please help me on that ??

Arnoo001
 
Posts: 2
Joined: Fri Jul 02, 2021 10:04 am

Mon Jul 05, 2021 9:56 am

Hello Arnab,

Thank you for your inquiry.
Please refer to the following code to achieve your requirement. If there is any question, please feel free to contact us.
Code: Select all
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx");
            //Access the first section
            Section section = doc.Sections[0];
            //Access the first table
            Table table = section.Tables[0] as Table;
                 
            //Add a new column
            AddColumn(table); 
           
            //Save the Word file
            string output = "output.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);
        }
        private static void AddColumn(Table table)
        {
            string header = "NewColumn";
            string[] data = { "e-iceblue", "e-iceblue", "e-iceblue", "e-iceblue", "e-iceblue", "e-iceblue", "e-iceblue" };

            for (int r = 0; r < table.Rows.Count; r++)
            {
                TableRow row = table.Rows[r];
                TableCell addCell = new TableCell(table.Document);
                row.Cells.Add( addCell);               
                    if (row.IsHeader)
                    {
                        addCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                        Paragraph p = addCell.AddParagraph();
                        p.Format.HorizontalAlignment = HorizontalAlignment.Center;
                        TextRange txtRange = p.AppendText(header);
                        txtRange.CharacterFormat.Bold = true;
                    }
                    else
                    {
                        for (int d = 0; d < data.Length; d++)
                        {
                            if (d + 1 == r)
                            {
                                addCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                                addCell.AddParagraph().AppendText(data[d]);
                            }
                        }
                    }             
            }
        }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Mon Jul 05, 2021 11:44 am

Hey ,


Thanks for your response , its working perfectly .

Thank You Again

Arnoo001
 
Posts: 2
Joined: Fri Jul 02, 2021 10:04 am

Tue Jul 06, 2021 2:14 am

Hello,

You're welcome.
If you have other questions about using Spire.Doc in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc