PHP导出Excel是指使用PHP编程语言将数据导出为Excel文件格式(如.xls或.xlsx)。这通常用于将数据库中的数据、网页上的表格数据等导出为Excel文件,以便用户可以在Excel中进行进一步处理和分析。
以下是一个使用PHP导出XLSX文件的简单示例:
<?php
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="export.xlsx"');
header('Cache-Control: max-age=0');
$data = [
['Name', 'Age', 'City'],
['John Doe', 30, 'New York'],
['Jane Smith', 25, 'Los Angeles'],
['Mike Johnson', 40, 'Chicago']
];
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
foreach ($data as $row => $rowData) {
foreach ($rowData as $col => $cellData) {
$sheet->setCellValueByColumnAndRow($col + 1, $row + 1, $cellData);
}
}
$writer->save('php://output');
?>PhpOffice\PhpSpreadsheet。通过以上方法,可以有效地解决PHP导出Excel过程中遇到的常见问题。