!!!!不要用ajax请求下载,ajax没有数据流形式的数据,需要使用的话要模拟表单请求
前端使用window.location.href直接请求页面即可
public function process(Vtiger_Request $request)
{
$mul_class_id = $request->get('productinstanceid');
$sql = " XXXXXXX ";
$arrayName[] = array(
'0' => '学号',
'1' => '学员姓名',
'2' => '性别',
'3' => '学校',
'4' => '年级',
'5' => '跟进人',
'6' => '跟进人团队',
'7' => '跟进人校区',
'8' => '状态',
'9' => '授课信息',
'10' => '开班成绩'
);
//获取需要导出的数据
$info = ProductInstance_Record_Model::getDownLoadMsg($sql);
foreach($info as $k => $v){
$arrayName[] = array(
'0' => $v['smpid'],
'1' => $v['accountname'],
'2' => $v['account_type'],
'3' => $v['campus'],
'4' => $v['grade'],
'5' => $v['last_name'],
'6' => $v['team'],
'7' => $v['campusid'],
'8' => $v['statusname'],
'9' => $v['teach'],
'10' => $v['score']
);
}
//p($arrayName)调用方法导出
$file_name = date('Y-m-d',time()).'-'.$mul_class_id;
$this->create_xls($arrayName,$file_name);
}
//导出excel的方法
function create_xls($data,$filename='student_info.xls'){ ini_set('max_execution_time', '0'); require_once("libraries/PHPExcel/PHPExcel.php"); $filename=str_replace('.xls', '', $filename).'.xls'; $phpexcel = new PHPExcel(); $phpexcel->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"); $phpexcel->getActiveSheet()->fromArray($data); $phpexcel->getActiveSheet()->setTitle('Sheet1'); $phpexcel->setActiveSheetIndex(0); ob_end_clean(); header('Content-Type: application/vnd.ms-excel'); /*header("content-type:text/html;charset=utf-8"); header("content-type:application/octem-stream");*/ header("Content-Disposition: attachment;filename=$filename"); 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 ob_clean(); $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5'); $objwriter->save('php://output'); exit; }