<?php require_once 'PHPExcel/Classes/PHPExcel.php'; /** * 导出数据为Excel * @param array $fieldArr 标题数组 * @param array $dataArr 数据数组 */ function exportExcel($fieldArr, $dataArr) { if (empty($fieldArr) || empty($dataArr)) { return; } //创建PHPExcel对象 $obj = new PHPExcel(); //创建excel写入对象 $writer = new PHPExcel_Writer_Excel5($obj); //得到当前工作表对象 $curSheet = $obj->getActiveSheet(); $fieldNum = count($fieldArr); $dataRow = count($dataArr) + 2; for($col = 0; $col < $fieldNum; ++$col) { $cellName = PHPExcel_Cell::stringFromColumnIndex($col) . '1'; $curSheet->setCellValue($cellName, $fieldArr[$col]); } for($row = 2; $row < $dataRow; ++$row) { for($col = 0; $col < $fieldNum; ++$col) { $cellName = PHPExcel_Cell::stringFromColumnIndex($col) . $row; $curSheet->setCellValue($cellName, $dataArr[$row-2][$col]); } } $fileName = date('YmdHis', time()); header('Content-Type: application/vnd.ms-execl'); header('Content-Disposition: attachment;filename="' . $fileName . '.xls"'); header('Cache-Control: max-age=0'); $writer->save('php://output'); }