Wiki

Clone wiki

Aspose for OpenXML / Adding Tables to Word Using OpenXML SDK

Adding Tables to Word Using OpenXML SDK

To use the code from the Open XML SDK 2.0, you must add several references to your project. The sample project already includes these references, but in your own code, you would have to explicitly reference the following assemblies:

  • WindowsBase:This reference may be set for you, depending on the kind of project that you create.
  • DocumentFormat.OpenXml:Installed by the Open XML SDK 2.0. In addition, you should add the following using/Imports statements to the top of your code file.
#!c#
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

Create a procedure "AddTable" which accepts two parameters "filename,data". This is how you'll pass the data to the function

#!c#
const string fileName = @"C:\temp\AddTable.docx";
AddTable(fileName, new string[,] 
  { { "Texas", "TX" }, 
     { "California", "CA" }, 
     { "New York", "NY" }, 
     { "Massachusetts", "MA" } });

Method Description

#!c#

public static void AddTable(string fileName, string[,] data)
{
  using (var document = WordprocessingDocument.Open(fileName, true))
  {

    var doc = document.MainDocumentPart.Document;

    Table table = new Table();

    TableProperties props = new TableProperties(
      new TableBorders(
        new TopBorder
        {
          Val = new EnumValue<BorderValues>(BorderValues.Single),
          Size = 12
        },
        new BottomBorder
        {
          Val = new EnumValue<BorderValues>(BorderValues.Single),
          Size = 12
        },
        new LeftBorder
        {
          Val = new EnumValue<BorderValues>(BorderValues.Single),
          Size = 12
        },
        new RightBorder
        {
          Val = new EnumValue<BorderValues>(BorderValues.Single),
          Size = 12
        },
        new InsideHorizontalBorder
        {
          Val = new EnumValue<BorderValues>(BorderValues.Single),
          Size = 12
        },
        new InsideVerticalBorder
        {
          Val = new EnumValue<BorderValues>(BorderValues.Single),
          Size = 12
        }));
    table.AppendChild<TableProperties>(props);

    for (var i = 0; i <= data.GetUpperBound(0); i++)
    {
      var tr = new TableRow();
      for (var j = 0; j <= data.GetUpperBound(1); j++)
      {
        var tc = new TableCell();
        tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
        // Assume you want columns that are automatically sized.
        tc.Append(new TableCellProperties(
          new TableCellWidth { Type = TableWidthUnitValues.Auto }));
        tr.Append(tc);
      }
      table.Append(tr);
    }
    doc.Body.Append(table);
    doc.Save();
  }
}

Adding Tables to Word Using Aspose SDK

#!c#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Words;

namespace InsertTableUsingDocumentBuilder
{
    class Program
    {
        static void Main(string[] args)
        {
            string projectFiles = Path.GetFullPath("../../Files/");

            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // We call this method to start building the table.
            builder.StartTable();
            builder.InsertCell();
            builder.Write("Row 1, Cell 1 Content.");

            // Build the second cell
            builder.InsertCell();
            builder.Write("Row 1, Cell 2 Content.");
            // Call the following method to end the row and start a new row.
            builder.EndRow();

            // Build the first cell of the second row.
            builder.InsertCell();
            builder.Write("Row 2, Cell 1 Content");

            // Build the second cell.
            builder.InsertCell();
            builder.Write("Row 2, Cell 2 Content.");
            builder.EndRow();

            // Signal that we have finished building the table.
            builder.EndTable();

            // Save the document to disk.
            doc.Save(projectFiles + "DocumentBuilder.CreateSimpleTable Out.doc");

            System.Console.WriteLine("File Saved Successfully.");

            System.Console.ReadKey();


        }
    }
}
Source: https://bitbucket.org/asposemarketplace/aspose-for-openxml/src/ffec3cde9d87/Aspose.Words/InsertTableUsingDocumentBuilder/?at=master

Updated