title: Export Datatable to Word Document With Page Numbers using Open XML
body: <p>My requirement is : Exporting a dynamic DataTable to Word document with Page Numbers
  We need to use Open XML to achieve this. 
  I have code to export Datatable to Word. And also to insert page numbers.</p>
  
  <p>I got Below code code to export datatable</p>
  
  <pre><code>public void CreateWordtable(string filename,DataTable data)
  {
      WordprocessingDocument doc = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document);
      MainDocumentPart mainDocPart = doc.AddMainDocumentPart();
      mainDocPart.Document = new Document();
      Body body = new Body();
      mainDocPart.Document.Append(body);
      //rinks@::creating new table
  
      DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();            
      for (int i = 0; i &lt; data.Rows.Count; ++i)
      {
          TableRow row = new TableRow();
          for (int j = 0; j &lt; data.Columns.Count; j++)
          {
              TableCell cell = new TableCell();
              cell.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data.Rows[i][j].ToString()))));
              cell.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1200" }));
              row.Append(cell);
          }
          table.Append(row);
      }
      body.Append(table);
      doc.MainDocumentPart.Document.Save();
      doc.Dispose();
  }
  </code></pre>
  
  <p>And below code is to insert page numbers in a word document</p>
  
  <pre><code>private static void AddPageNumberFooters(WordprocessingDocument parent)
  {
      string documentPath = @"D:\EmptyDoc.docx";
      using (WordprocessingDocument package =
          WordprocessingDocument.Open(documentPath, true))
      {
          var mainDocumentPart = parent.AddMainDocumentPart();
          GenerateMainDocumentPart().Save(mainDocumentPart);
  
          var documentSettingsPart =
              mainDocumentPart.AddNewPart
              &lt;DocumentSettingsPart&gt;("rId1");
  
          GenerateDocumentSettingsPart().Save(documentSettingsPart);
  
          var firstPageFooterPart = mainDocumentPart.AddNewPart&lt;FooterPart&gt;("rId1");
          GeneratePageFooterPart("Page 1 of 2").Save(firstPageFooterPart);
  
          var secondPageFooterPart = mainDocumentPart.AddNewPart&lt;FooterPart&gt;("rId2");
          GeneratePageFooterPart("Page 2 of 2").Save(secondPageFooterPart);
      }
  }
  
  
  private static Document GenerateMainDocumentPart()
  {
      var element =
          new Document(
              new Body(
                  new Paragraph(
                      new Run(
                          new Text("Page 1 content"))
                  ),
                  new Paragraph(
                      new Run(
                          new Break() { Type = BreakValues.Page })
                  ),
                  new Paragraph(
                      new Run(
                          new LastRenderedPageBreak(),
                          new Text("Page 2 content"))
                  ),
                  new Paragraph(
                      new Run(
                          new Break() { Type = BreakValues.Page })
                  ),
                  new SectionProperties(
                      new FooterReference()
                      {
                          Type = HeaderFooterValues.First,
                          Id = "rId1"
                      },
                      new FooterReference()
                      {
                          Type = HeaderFooterValues.Even,
                          Id = "rId2"
                      },
                      new PageMargin()
                      {
                          Top = 1440,
                          Right = (UInt32Value)1440UL,
                          Bottom = 1440,
                          Left = (UInt32Value)1440UL,
                          Header = (UInt32Value)720UL,
                          Footer = (UInt32Value)720UL,
                          Gutter = (UInt32Value)0UL
                      },
                      new TitlePage()
                  )));
  
      return element;
  }
  
  
  private static Header GeneratePageHeaderPart(string HeaderText)
  {
      var element =
          new Header(
              new Paragraph(
                  new ParagraphProperties(
                      new ParagraphStyleId() { Val = "Header" }),
                  new Run(
                      new Text(HeaderText))
              ));
  
      return element;
  }
  </code></pre>
  
  <p>My problem is, I have combine above both methods to export data along with page numbers.
  if we know there are 2 pages in the word document, i can insert 2 FooterParts.
  But i don't know how many pages will be created after exporting the data.</p>