Tables are helpful to organize information and figures. We often insert tables in word documents (DOCX/DOC) to show information. In a word processing application, you can easily create tables using C++. You can go through the following examples to learn working with Tables in Word documents:
- Insert Tables in Word Documents API
- Insert a Table in Word Documents using C++
- Insert Table from HTML in Word Documents using C++
- Insert Table in DOCX Using Document Builder in C++
Let us move on to explore all these topics in detail:
Insert Tables in Word Documents API
Firstly, please note that you will be using Aspose.Words for C++ API to insert tables in a word document. You can configure the API by downloading it from New Releases or via the NuGet gallery. Once configured properly, you can simply utilize the methods, properties, and classes exposed by the API so that a few simple API calls can be used to create, edit or manipulate Microsoft Word documents, like DOCX or DOC files.
Insert a Table in Word Documents using C++
You can insert a table in word documents with a few simple steps. However, it is important to note here that you must pass the document object to the constructor of each node so that all child nodes belong to the same object. You need to follow the steps listed below:
- Initialize object of Document class
- Create Table object
- Add the Table to Document
- Create Rows and Columns
- Apply AutoFit on Table Cells
- Save output Word Document
The code snippet below shows how to insert a Table in Word document (DOCX/DOC) using C++:
Insert Table from HTML in Word Documents using C++
It is possible that an HTML file contains Table that you need to insert into your word documents like DOCX, DOC, etc. Or you may need to copy a table from a website. So instead of creating and designing the table from scratch, you can easily parse HTML markup as a table into word document. For example, you can add a table into word document using the following HTML string:
<table><tr><td>Row 1, Cell 1</td><td>Row 1, Cell 2</td></tr><tr><td>Row 2, Cell 1</td><td>Row 2, Cell 2</td></tr></table>
We have kept the contents simple so that the support for table tag can be demonstrated with basic yet important use case. Moreover, it is important to note here that AutoFit can not be applied on tables which are created from HTML.
Let us follow the steps below for inserting HTML table in Word document:
- Initialize an instance of Document class
- Pass the HTML markup with InsertHtml method
- Save output DOCX word file
Below code follows these steps and shows how to create table in Word document with HTML using C++:
You can notice this method is a bit simpler than the approach we have explored above. The reason being, you do not need to add each node one by one for the rows, columns, or cells because the Table tag in the HTML string contains all information. Following is a screenshot of this simple HTML table added into the Word document:
Insert Table Using Document Builder in C++
The best thing about Aspose.Words for C++ API is that it offers a variety of features that become a competitive edge for the API and make it stand out among other options. Likewise, the feature of the inserting table using a document builder is another approach of adding tables in word documents (DOC/DOCX). So let us explore the details from three different perspectives:
1) Insert Simple Table in DOCX with Document Builder using C++
For adding a simple table in word document with Document builder, you need to follow the steps below:
- Create Document Object
- Call StartTable() method and insert cells
- Add the Row and Cells
- Save output DOCX file
Moreover, the code snippet below shows how to insert simple table in DOCX file with C++:
2) Insert Formatted Table in DOCX with Document Builder using C++
You can insert a formatted table into a word document with the steps below:
- Initialize an instance of Document class
- Make Header Row
- Set indents and features for the formatting
- Reset Font formatting
- Save output Word DOCX file
Below code snippet creates formatted table in DOCX file using C++:
3) Insert Nested Table in DOCX with Document Builder using C++
Sometimes we need another table inside an existing table. For instance, a cell in some row or column of a table can contain a sub-table for a sub-category or some other field. In such scenarios, Nested tables are helpful which can be added by following the steps below:
- Build outer Table and then call EndTable method
- Build inner Table inside a cell of outer table
- Save output word document
The following code snippet shows how to insert nested table in Word document using C++:
Conclusion
To sum up, we have learned how to insert different tables in Word documents using different approaches. You can insert simple table or with HTML string as well as the formatted and nested tables into the word documents (DOC/DOCX). However, in case of any confusion or query, you can reach out to us at Free Support Forums. Moreover, you can also refer to API References and Product Documentation for your kind reference.
See Also
Add or Remove Header and Footer in Word Documents using Java