Пользуюсь библиотекой PHPExcel.
require_once 'Classes/PHPExcel.php';
$phpExcel = new PHPExcel();
$res = array(
'0' => array(
'data' => '20.11.2003',
'name' => 'Иванов П.С',
'mail' => 'ivano@exemle.com'
),
'1' => array(
'data' => '20.13.2005',
'name' => 'Сидоров П.А',
'mail' => 'sidorov@exemle.com'
),
'2' => array(
'data' => '23.11.2008',
'name' => 'Петров П.С',
'mail' => 'petrov@exemle.com'
)
);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$i = null;
foreach($res as $val)
{
$i++;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue("A$i", $val[data]);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue("B$i", $val[name]);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue("C$i", $val[mail]);
}
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="stat.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');