PHP中常用的導(dǎo)出數(shù)據(jù)的方法有:
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');
echo "Name,Age\n";
echo "Alice,25\n";
echo "Bob,30\n";
$file = fopen('data.csv', 'w');
fputcsv($file, array('Name', 'Age'));
fputcsv($file, array('Alice', 25));
fputcsv($file, array('Bob', 30));
fclose($file);
echo '<a href="data.csv" download>Download CSV</a>';
require 'PHPExcel.php';
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0)
->setCellValue('A1', 'Name')
->setCellValue('B1', 'Age')
->setCellValue('A2', 'Alice')
->setCellValue('B2', 25)
->setCellValue('A3', 'Bob')
->setCellValue('B3', 30);
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$writer->save('data.xlsx');
echo '<a href="data.xlsx" download>Download Excel</a>';
這些方法可以根據(jù)需要選擇合適的方式來導(dǎo)出數(shù)據(jù)。