Export Excel made easy
To create an export file with php you can simply print a string with the HTML formatted data.
As Excel can read HTML or at least HTML tables, everything is easy and already done for you, you can even create css styles on your cells and even write formulas.
You can do as follow:
- Create an Html page with a link that points to, lets say, excel_simple.php
- Include the following pre
- Click on your link to open
<?php
// excel_simple.php Print Excel Simple
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=test_".date("Y-m-d").".xls");
header("Pragma: no-cache");
header("Expires: 0");
print "<TABLE><tr><th bgcolor=#00CCFF>Col. 1</th><th bgcolor=#00CCFF>Col. 2</th><th bgcolor=#00CCFF>Col. 3</th></tr>
<TR><TD>1</TD><TD>2</TD><TD>3</TD></TR>
<TR><TD bgcolor=silver>4</TD><TD bgcolor=silver>4</TD><TD bgcolor=silver>6</TD></TR>
<TR><TD>7</TD><TD>8</TD><TD>9</TD></TR>
<TR><TD bgcolor=orange>=SUM(A1:A3)</TD><TD bgcolor=orange>=SUM(B1:B3)</TD><TD bgcolor=orange>=SUM(C1:C3)</TD></TR>
</TABLE>";
?>
Which gives :
Col. 1 | Col. 2 | Col. 3 |
---|---|---|
1 | 2 | 3 |
4 | 4 | 6 |
7 | 8 | 9 |
=SUM(A1:A3) | =SUM(B1:B3) | =SUM(C1:C3) |
That can be opened with Excel