How to generate an .xlsx using php
<p>I need to generate an Excel file with extension <code>.xlsx</code>.</p>
<p>Here is my simple code:</p>
<pre><code> $file = &quot;test.xlsx&quot;;
 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment; filename='.$file);
 $content = &quot;Col1\tCol2\tCol3\t\n&quot;;
 $content .= &quot;test1\ttest1\ttest3\t\n&quot;;
 $content .= &quot;testtest1\ttesttest2\ttesttest3\t\n&quot;;
 echo $content;
</code></pre>
<p>But I get this error when I open the generated file:</p>
<blockquote>
<p><code>Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid.</code></p>
</blockquote>
<p>Any ideas?</p>