title: Insert table in .docx file using ZipArchive PHP
body: <p>I want to create a table in a docx file using PHP and ZipArchive. But it isn't really working.</p>
  <p>I have the following .docx file:
  <a href="https://i.stack.imgur.com/RzBtt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RzBtt.png" alt="Word document template PHP" /></a></p>
  <p>I will put the content here using ZipArchive like the following:</p>
  <pre class="lang-php prettyprint-override"><code>    $zip = new ZipArchive;
  
      $my_table = &quot;
      ...
      &quot;;
  
      if ($zip-&gt;open(&quot;document.docx&quot;) === TRUE) {
          $template_content = $zip-&gt;getFromName(&quot;word/document.xml&quot;);
  
          $new_content = str_replace(&quot;{{ my_table }}&quot;, $my_table, $template_content);
  
  
          $zip-&gt;addFromString(&quot;word/document.xml&quot;, $new_content);
          $zip-&gt;close();
      }
  </code></pre>
  <p>I don't know what to put in the PHP variable <code>$my_table</code>. I tried a <strong>HTML table</strong> like this:</p>
  <pre class="lang-html prettyprint-override"><code>$my_table = '
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;H1&lt;/th&gt;
              &lt;th&gt;H2&lt;/th&gt;
              &lt;th&gt;H3&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;a&lt;/td&gt;
              &lt;td&gt;b&lt;/td&gt;
              &lt;td&gt;c&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;a&lt;/td&gt;
              &lt;td&gt;b&lt;/td&gt;
              &lt;td&gt;c&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;a&lt;/td&gt;
              &lt;td&gt;b&lt;/td&gt;
              &lt;td&gt;c&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  ';
  </code></pre>
  <p>Output:
  <a href="https://i.stack.imgur.com/QSA4L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QSA4L.png" alt="HTML table output from PHP" /></a></p>
  <p>I also tried the <strong>Open Office XML Standard</strong>:</p>
  <pre class="lang-html prettyprint-override"><code>$my_table = '
      &lt;w:tbl&gt;
          &lt;w:tblPr&gt;
              &lt;w:tblStyle w:val=&quot;TableGrid&quot;/&gt;
              &lt;w:tblW w:w=&quot;5000&quot; w:type=&quot;pct&quot;/&gt;
          &lt;/w:tblPr&gt;
          &lt;w:tblGrid&gt;
              &lt;w:gridCol w:w=&quot;2880&quot;/&gt;
              &lt;w:gridCol w:w=&quot;2880&quot;/&gt;
              &lt;w:gridCol w:w=&quot;2880&quot;/&gt;
          &lt;/w:tblGrid&gt;
          &lt;w:tr&gt;
          &lt;w:tc&gt;
          &lt;w:tcPr&gt;
              &lt;w:tcW w:w=&quot;2880&quot; w:type=&quot;dxa&quot;/&gt;
          &lt;/w:tcPr&gt;
          &lt;w:p&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;AAA&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;/w:tc&gt;
          &lt;w:tc&gt;
          &lt;w:tcPr&gt;
          &lt;w:tcW w:w=&quot;2880&quot; w:type=&quot;dxa&quot;/&gt;
          &lt;/w:tcPr&gt;
          &lt;w:p&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;BBB&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;/w:tc&gt;
          &lt;w:tc&gt;
          &lt;w:tcPr&gt;
              &lt;w:tcW w:w=&quot;2880&quot; w:type=&quot;dxa&quot;/&gt;
          &lt;/w:tcPr&gt;
          &lt;w:p&gt;
              &lt;w:r&gt;
                  &lt;w:t&gt;CCC&lt;/w:t&gt;
              &lt;/w:r&gt;
          &lt;/w:p&gt;
          &lt;/w:tc&gt;
          &lt;/w:tr&gt;
      &lt;/w:tbl&gt;
  ';
  </code></pre>
  <p>Output:
  <a href="https://i.stack.imgur.com/9JBxo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9JBxo.png" alt="Open Office XML standard" /></a></p>
  <p>I also tried to create a table in Word, and extract the Open Office XML from that Word-file. But also that gave the same kind of output.</p>
  <p>So what would I place in the <code>$my_table</code> variable to get a nice table with borders in my output file?</p>
  <p>Thanks!</p>