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.

Mon Dec 09, 2013 12:25 am

Hi, I was wondering how to:
1. In an existing document with a 3 column table with one row, find the table; and
2. Add more rows to the table (the number of rows will derive from a table in a database).
3. Is it possible to set individual borders on a table cell?
Any help would be greatly appreciated.
Chris

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Mon Dec 09, 2013 2:29 am

Hello,

Thanks for your inquiry.
Please kindly refer to the following code snippet.
Code: Select all
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"..\..\test.docx", FileFormat.Docx);

           //Get all tables from document
            List<Table> tables = FindTable(document);

            //Get the first table from document
            Table table = tables[0];

            //Get a datatable from database
            DataTable dataTable = GetDataTableFromDB();

            //Add rows to table
            for(int i=0;i<dataTable.Rows.Count;i++)
            {
                table.AddRow();
            }

           TableCell cell= table.Rows[0].Cells[0];

            //Set all borders for tableCell
           cell.CellFormat.Borders.LineWidth = 1;

           cell = table.Rows[0].Cells[1];

           //Set right border for tableCell
           cell.CellFormat.Borders.Right.LineWidth = 1;

           document.SaveToFile("sample.docx",FileFormat.Docx);
        }
        static List<Table> FindTable(Document document)
        {
            List<Table> tables = new List<Table>();
            foreach (Section section in document.Sections)
            {
                if (section.Tables.Count > 0)
                {
                    tables.AddRange(section.Tables.Cast<Table>().ToArray());
                }
            }
            return tables;
        }


Welcome to write to us again for further problems.

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Dec 13, 2013 9:19 am

Hello,

Have you tried the code snippet? Did it resolve your issue?
Thanks for your feedback.

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Dec 18, 2013 10:37 am

Hi, this worked perfectly. Thank you so much.
Chris

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Thu Dec 19, 2013 1:22 am

Dear Chris,

Thanks for your feedback.
Welcome to write to us again for further problems.

Best regards and have a nice day,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc