[英]PHP: Exporting and importing MySQL table to excel
我正在尝试将MySql数据库中的表导出到PHP。 但是我收到文件格式或文件扩展名无效的错误。 我当前的代码如下:
$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;
在此先感谢您的帮助。
将文件名的扩展名从.xlsx
更改为.xls
。
如果之后仍然遇到问题,则注册表中的扩展名可能具有不正确的MIME类型。 维修步骤和更多信息,请参见以下链接。
Microsoft Docs: 文件类型:设置子项和文件类型扩展名属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.