<?php
require_once(BasePath.DS.'vendor'.DS.'PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->createSheet();//创建新的内置表
$objSheet = $objPHPExcel->getActiveSheet();//获取当前活动sheet
$objSheet->setTitle($name); //给sheet 标题命名
#标题行
$objSheet ->setCellValue("A1", "标题1")
->setCellValue("B1", "标题2")
->setCellValue("C1", "标题3");
$i = 2;
foreach($data as $item){
$objSheet ->setCellValue("A".$i, $item['data1'])
->setCellValue("B".$i, $item['data2'])
->setCellValue("C".$i, $item['data3']);
$i++;
}
$fileName = 'test.xls';
header("Pragma: public");header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename="'.$fileName.'"');
header("Content-Transfer-Encoding:binary");
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');
**********附注:不使用ajax直接请求,因为ajax请求需要有数据返回与header冲突,假如使用ajax直接请求,返回的结果是一堆乱码********