duanpaxin3531 2018-07-07 22:01
浏览 82

PHP:导出和导入MySQL表到excel

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.

  • 写回答

1条回答 默认 最新

报告相同问题?