HTML Table to Excel File Tutorial
Filed Under (General) by Wenbert on 02-10-2007
Tagged Under : excel, PHP
Here is a tutorial on how to export something from your database to an excel file using PHP. It is useful when you want to download a report.
Basically, it displays an HTML table but instead of showing it in the browser, we have set the header to treat the output as an excel file.
-
$dbhost = ‘localhost’;
-
$dbuser = ‘root’;
-
$dbpass = ‘xxx’;
-
-
-
$dbname = ‘employee’;
-
-
$query = ‘SELECT
-
*
-
FROM employee_table’;
-
-
-
-
echo ‘
-
<table border="1">’;
-
echo ‘
-
<tbody>
-
<tr>’;
-
echo ‘
-
<td><strong>Firt Name</strong></td>
-
‘;
-
echo ‘
-
<td><strong>Last Name</strong></td>
-
‘;
-
echo ‘
-
<td><strong>Username</strong></td>
-
‘;
-
echo ‘
-
<td><strong>Date Hired</strong></td>
-
‘;
-
echo ‘
-
<td><strong>Marital Status</strong></td>
-
‘;
-
-
echo ‘</tr>
-
<tr>’;
-
echo ‘
-
<td>’.$row[‘firstname’].‘</td>
-
‘;
-
echo ‘
-
<td>’.$row[‘lastname’].‘</td>
-
‘;
-
echo ‘
-
<td>’.$row[‘username’].‘</td>
-
‘;
-
echo ‘
-
<td>’.$row[‘datehired’].‘</td>
-
‘;
-
echo ‘
-
<td>’.$row[‘maritalstatus’].‘</td>
-
‘;
-
echo ‘</tr>
-
‘;
-
}
-
-
echo ‘</tbody></table>
-
‘;