Simple Table

Tables are used to organize document content in rows and columns. In GemBox.Document, table related types are contained in a separate GemBox.Document.Tables namespace.

Following sample demonstrates how to create and populate a simple table by constructing all table related elements – table, table rows and table cells.

Screenshot

Simple Table Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using System.Data;
3using GemBox.Document;
4using GemBox.Document.Tables;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = new DocumentModel();
15
16        int tableRowCount = 10;
17        int tableColumnCount = 5;
18
19        Table table = new Table(document);
20        table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
21
22        for (int i = 0; i < tableRowCount; i++)
23        {
24            TableRow row = new TableRow(document);
25            table.Rows.Add(row);
26
27            for (int j = 0; j < tableColumnCount; j++)
28            {
29                Paragraph para = new Paragraph(document, string.Format("Cell {0}-{1}", i + 1, j + 1));
30
31                row.Cells.Add(new TableCell(document, para));
32            }
33        }
34
35        document.Sections.Add(new Section(document, table));
36
37        document.Save("Simple Table.docx");
38    }
39}
1Imports System
2Imports System.Data
3Imports GemBox.Document
4Imports GemBox.Document.Tables
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = New DocumentModel
14
15        Dim tableRowCount As Integer = 10
16        Dim tableColumnCount As Integer = 5
17
18        Dim table As New Table(document)
19        table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)
20
21        For i As Integer = 0 To tableRowCount - 1
22            Dim row As New TableRow(document)
23            table.Rows.Add(row)
24
25            For j As Integer = 0 To tableColumnCount - 1
26                Dim para As New Paragraph(document, String.Format("Cell {0}-{1}", i + 1, j + 1))
27
28                row.Cells.Add(New TableCell(document, para))
29            Next
30        Next
31
32        document.Sections.Add(New Section(document, table))
33
34        document.Save("Simple Table.docx")
35
36    End Sub
37
38End Module

Check next sample or find out more about GemBox.Document and GemBox Software.