Create Word Table in WPF with C#

2016-03-03 08:15:39 Written by  support iceblue
Rate this item
(0 votes)

A table is a great way to display data into group, frequently used in our financial reports, academic reports and any other documents that contain a group of similar data. In this article, I am fusing on how to insert a table in Word, fill the table with data and how to format the cells using Spire.Doc for WPF with C#.

Code Snippets

Step 1: Initialize a new instance of Document class, add a section to it.

Document doc = new Document();
Spire.Doc.Section s = doc.AddSection();

Step 2: Define the data that will be filled with table.

String[] Header ={"Vendor No","Vendor Name", "Address","City","State","Zip"};  
String[][] data = {
                      new String[]{"3501","Cacor Corporation","161 Southfield Rd","Southfield","OH","60093"},
                      new String[]{"3502","Underwater","50 N3rd Street","Indianapolis","IN","46208"},
                      new String[]{"3503","J.W. Luscher Mfg.","65 Addams Street","Berkely","MA","02779"},
                      new String[]{"3504","Scuba Professionals","3105 East Brace","Rancho Dominguez","CA","90221"},
                      new String[]{"3505","Divers' Supply Shop", "5208 University Dr","Macon","GA","20865"},
                      new String[]{"3506","Techniques","52 Dolphin Drive","Redwood City","CA","94065-1086"},
                  };

Step 3: Add a table with border to section, set the rows and columns number based on the data length.

Spire.Doc.Table table = s.AddTable(true);
table.ResetCells(data.Length + 1, Header.Length);

Step 4: Fill the table with the predefined data and format the cells.

Spire.Doc.TableRow FRow= table.Rows[0];
FRow.IsHeader = true;
FRow.Height = 23;
FRow.RowFormat.BackColor = System.Drawing.Color.DarkBlue;
for (int i = 0; i < Header.Length; i++)
{
    //Cell Alignment
    Paragraph p = FRow.Cells[i].AddParagraph();
    FRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
    p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
    //Fill Header data and Format the Cells
    TextRange TR = p.AppendText(Header[i]);
    TR.CharacterFormat.FontName = "Calibri";
    TR.CharacterFormat.FontSize = 12;
    TR.CharacterFormat.TextColor = System.Drawing.Color.White;
    TR.CharacterFormat.Bold = true;
}
for (int r = 0; r < data.Length; r++)
{
    TableRow DataRow = table.Rows[r + 1];
    //Row Height
    DataRow.Height = 15;
    //C Represents Column.
    for (int c = 0; c < data[r].Length; c++)
    {
        //Cell Alignment
        DataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
        //Fill Data in Rows
        Paragraph p2 =DataRow.Cells[c].AddParagraph();
        TextRange TR2=p2.AppendText(data[r][c]);
        //Format Cells
        p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
        TR2.CharacterFormat.FontName = "Calibri";
        TR2.CharacterFormat.FontSize = 10;
        TR2.CharacterFormat.TextColor = System.Drawing.Color.Black;
    }
}

Step 5: Call AutoFitBebavior() method to make the table AutoFit to Window.

table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);

Step 6: Save and launch the file.

doc.SaveToFile("WordTable.docx");
System.Diagnostics.Process.Start("WordTable.docx");

Output:

Create Word Table in WPF with C#

Full Code:

private void button1_Click(object sender, RoutedEventArgs e)
{
    Document doc = new Document();
    Spire.Doc.Section s = doc.AddSection();
    
    String[] Header ={"Vendor No","Vendor Name", "Address","City","State","Zip"};  
    String[][] data = {
                          new String[]{"3501","Cacor Corporation","161 Southfield Rd","Southfield","OH","60093"},
                          new String[]{"3502","Underwater","50 N3rd Street","Indianapolis","IN","46208"},
                          new String[]{"3503","J.W. Luscher Mfg.","65 Addams Street","Berkely","MA","02779"},
                          new String[]{"3504","Scuba Professionals","3105 East Brace","Rancho Dominguez","CA","90221"},
                          new String[]{"3505","Divers' Supply Shop", "5208 University Dr","Macon","GA","20865"},
                          new String[]{"3506","Techniques","52 Dolphin Drive","Redwood City","CA","94065-1086"},
                      };
    Spire.Doc.Table table = s.AddTable(true);
    table.ResetCells(data.Length + 1, Header.Length);   
    Spire.Doc.TableRow FRow= table.Rows[0];
    FRow.IsHeader = true;
    FRow.Height = 23;
    FRow.RowFormat.BackColor = System.Drawing.Color.DarkBlue;
    for (int i = 0; i < Header.Length; i++)
    {
        Paragraph p = FRow.Cells[i].AddParagraph();
        FRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
        p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
        TextRange TR = p.AppendText(Header[i]);
        TR.CharacterFormat.FontName = "Calibri";
        TR.CharacterFormat.FontSize = 12;
        TR.CharacterFormat.TextColor = System.Drawing.Color.White;
        TR.CharacterFormat.Bold = true;
    }
    for (int r = 0; r < data.Length; r++)
    {
        TableRow DataRow = table.Rows[r + 1];
        DataRow.Height = 15;
        for (int c = 0; c < data[r].Length; c++)
        {
            DataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
            Paragraph p2 =DataRow.Cells[c].AddParagraph();
            TextRange TR2=p2.AppendText(data[r][c]);
            p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
            TR2.CharacterFormat.FontName = "Calibri";
            TR2.CharacterFormat.FontSize = 10;
            TR2.CharacterFormat.TextColor = System.Drawing.Color.Black;
        }
    }
    table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
    doc.SaveToFile("WordTable.docx");
    System.Diagnostics.Process.Start("WordTable.docx");
}

Additional Info

  • tutorial_title:
Last modified on Friday, 24 September 2021 09:51