public function exportCustomer() {
$Customer = D('Customer');
$list = $Customer->getCustomers();
$filename = "客户";
$headArr = array('订单ID', '用户ID', '商品ID', '商品名称', '商品型号', '经销商', '品牌', '商品单价', '数量', '收货人', '收货人电话', '省份', '城市', '地区', '详细地址');
exportExcel($filename, $headArr, $list, 'customer');
}
/** [exportExcel 导出数据到Excel]
*/
function exportExcel($fileName, $headArr, $data, $sheetname, $format = '') {
$datetime = date('Y-m-d His');
$fileName .= " {$datetime}.xls";

//创建PHPExcel对象,注意,不能少了\
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();

//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex(0);
$objActSheet = $objPHPExcel->getActiveSheet();
//设置当前活动sheet的名称
$objActSheet->setTitle($sheetname);

//设置表头
$key = ord("A");
foreach ($headArr as $v) {
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$key += 1;
}

$column = 2;
foreach ($data as $key => $rows) {
//行写入
$span = ord("A");
foreach ($rows as $keyName => $value) {
// 列写入
$j = chr($span);
$objActSheet->setCellValue($j . $column, $value);
$span++;
}
$column++;
}

// 设置格式
if (! empty($format) && is_array($format)) {
// 文字居中显示
if (isset($format['align']) && is_array($format['align'])) {
$alignArray = array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
);

foreach ($format['align'] as $val) {
// getStyle($value)->getAlignment()->applyFromArray($alignArray);
$objActSheet->getStyle($val)->getAlignment()->applyFromArray($alignArray);
}
unset($val);
}
// 设置列宽
if (isset($format['length']) && is_array($format['length'])) {
foreach ($format['length'] as $key => $val) {
$objActSheet->getColumnDimension($key)->setWidth($val);
}
unset($val);
}
}



// getColumnDimension('A')->setWidth(20);
//$objActSheet->getColumnDimension('A')->setWidth(50);

$fileName = iconv("utf-8", "gb2312", $fileName);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); //文件通过浏览器下载
exit;
}


vendor("PHPExcel.PHPExcel");

​点击下载​