I'm trying to export a table from MySql database to excel with PHP. But I'm getting the error that the file format or file extension is not valid. My current code is below:
$db = JFactory::getDBO();
$output = '';
$query = "SELECT * FROM student_management_module";
$db->setQuery($query);
$rows = $db->loadObjectList();
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Email</th>
<th>Program</th>
<th>Class</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
';
foreach ($rows as &$row) {
$output .= '
<tr>
<td>'.$row->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->program.'</td>
<td>'.$row->class.'</td>
<td>'.$row->start_date.'</td>
<td>'.$row->end_date.'</td>
<td>'.$row->student_id.'</td>
';
}
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename='download.xlsx");
header("Pragma: no-cache");
$output .= '</table>';
echo $output;
Thanks in advance, any help would be much appreciated.