Create Table in Word using C# | Create Nested Tables in C#

Tables in a Word document are a powerful tool for organizing and presenting data in a clear and structured format. A table consists of rows and columns, which intersect to form cells that can contain text, numbers, images, or other elements. In this article, we will learn how to create a table in Word documents programmatically using C#. This article shows various methods to create tables in a DOCX file supported by code samples.

This article covers the following topics:

C# Library to Create Tables in Word Documents

For working with tables in Word documents, we will use the Aspose.Words for .NET library. It is a robust library that enables you to dynamically create and manipulate Word documents programmatically directly within .NET applications.

Please download the DLL or install it from NuGet using the following command:

PM> Install-Package Aspose.Words

Create a Table in a Word Document in C#

There are two ways to create tables in Word documents using Aspose.Words for .NET:

  • Using the DocumentBuilder class
  • Using the DOM (Document Object Model)

You can choose the method that best fits your requirements. So let’s explore each of these methods in detail.

Create a Table using DocumentBuilder

The DocumentBuilder class enables efficient and effortless creation of dynamic documents from scratch or modification of existing ones. With its comprehensive range of functions, we can seamlessly insert diverse content elements, including text, checkboxes, OLE objects, paragraphs, lists, tables, images, and many more.

Please follow the steps below to create a table in a Word document using the DocumentBuilder class.

  1. Create an object of the Document class.
  2. Create an object of the DocumentBuilder class.
  3. Create a new table using the StartTable() method.
  4. Insert a cell using the InsertCell() method.
  5. Insert text into the cell using the Write() method.
  6. Repeat inserting cells and text into cells as required.
  7. End a row using the EndRow() method.
  8. End the table using the EndTable() method.
  9. Finally, save the Word document using the Save() method.

The following code sample shows how to create a table in a Word document using C#.

Create a Table in a Word using DocumentBuilder

Create a Table using DocumentBuilder

Create a Table using Document Object Model (DOM)

The Document Object Model (DOM) is an in-memory representation of a Word document. It enables programmatic access to read, manipulate, and modify the content and formatting of a Word document.

Please follow the steps below to create a table in a Word document using DOM.

  1. Create an object of the Document class.
  2. Create a new table using the Table() class.
  3. Add a table to the document body using the AppendChild() method.
  4. Create an object of the Row class and insert it into the table using Table.AppendChild(Row) method.
  5. Create an object of the Cell class, set formatting options, and add text to the cell.
  6. Insert the cell into the row using the Row.AppendChild(Cell) method.
  7. After that, repeat the process for as many rows as you want.
  8. Finally, save the Word document using the Save() method.

The following code sample shows how to create a table in a Word document using C#.

Create a Table using Document Object Model (DOM)

Create a Table using Document Object Model (DOM)

Create a Nested Table in a Word Document using C#

We can also create a new table within a cell of the table. The following are the steps to create a nested table in a Word document.

  1. Create an object of the Document class.
  2. Create an object of the DocumentBuilder class.
  3. Create a table using the StartTable() method and get a reference to the table in an object.
  4. Insert a cell using the InsertCell() method and get a reference to the cell in an object.
  5. Insert text into a cell using the DocumentBuilder.Write() method.
  6. Repeat inserting cells and text into cells as required.
  7. End the table when you have inserted all the rows.
  8. Move control inside the desired cell using the MoveTo(cell.FirstParagraph) method.
  9. Create another table by inserting cells, and end the table when done.
  10. Finally, save the Word document using the Save() method.

The following code sample shows how to create a nested table in a Word document using C#.

Create a Nested Table in a Word Document using C#

Create a Nested Table in a Word Document using C#

Clone an Existing Table in a Word Document in C#

We can clone an existing table in a Word document by following the steps below:

  1. Load an existing document with a table using the Document class.
  2. Get the table in an object using the GetChild(NodeType.TABLE, int, boolean) method.
  3. Clone a table using the Table.Clone(True) method.
  4. Insert a cloned table using the Table.ParentNode.InsertAfter() method.
  5. Insert an empty paragraph between tables using Table.ParentNode.InsertAfter(new Paragraph(Document), Table) method.
  6. Finally, save the Word document using the Save() method.

The following code sample shows how to clone a table in a Word document using C#.

Clone an Existing Table in a Word Document in C#

Clone an Existing Table in a Word Document in C#

Create a Table in a Word Document from HTML

We can also create a table in a Word document using an HTML string by following the steps below:

  1. Create an object of the Document class.
  2. Create an object of the DocumentBuilder class.
  3. Insert the HTML string of the table into the document using the DocumentBuilder.InsertHtml(String) method.
  4. Finally, save the document using Document.Save() method.

The following code sample shows how to insert an HTML table in a Word document using C#.

Create a Table in a Word Document from HTML

Create a Table in a Word Document from HTML

Get a Free API License

You can get a temporary license to use Aspose.Words for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to create tables in Word documents using C#. We have explored various methods to create tables programmatically using C#. We have also seen how to create nested tables or clone the existing tables in Word documents dynamically. Besides, you can learn more and explore variety of other features of the library using the documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also