我正在尝试从数据库中获取记录,然后将这些记录下载为Excel工作表。它会下载,但是当我打开excel表格时,我会看到html代码和一些乱码。我什至没有看到记录。我不知道问题出在哪里。这是我的php代码:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$res = $call->fetchRecords();
$num = $res->num_rows;
if($num > 0){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('records');
$i = 1;
while($row = mysqli_fetch_assoc($res)){
$sheet->setCellValue('A'.$i, $row['date']);
$sheet->setCellValue('B'.$i, $row['Reference']);
$sheet->setCellValue('C'.$i, $row['amount']);
$i++;
}
$writer = new Xlsx($spreadsheet);
if(isset($writer)){
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="pending_transactions.xlsx"');
header('Cache-Control: max-age=0');
header('Content-Transfer-Encoding: binary');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer->save('php://output');
}
}