实现代码:
private function exportExcel($params)
{
$arr = $this->getExportData($params); // 获取需要导出的信息(我这边是:二维数组)
$fileName = "5月文化衫信息采集表";
$xlsTitle = iconv('utf-8', 'gbk', "5月文化衫信息采集表"); //注意编码转换
require LIB_PATH . "application/library/PHPExcel/PHPExcel.php";
$objPHPExcel = new PHPExcel();
$objSheet = $objPHPExcel->getActiveSheet(); // 获取当前活动sheet
$objSheet->setTitle('5月文化衫信息采集表'); // 给当前的活动sheet起个名称
$objSheet->setCellValue('A1', '员工名');
$objSheet->setCellValue('B1', '工号');
$objSheet->setCellValue('C1', '一级部门');
$objSheet->setCellValue('D1', '二级部门');
$objSheet->setCellValue('E1', '衣服款式');
$objSheet->setCellValue('F1', '尺码');
$i = 2;
foreach ($arr as $key => $val) {
switch ($val['clothes_style']) {
case 1:
$val['clothes_style'] = '款式A';
break;
case 2:
$val['clothes_style'] = '款式B';
break;
}
switch ($val['clothes_size']) {
case 1:
$val['clothes_size'] = 'XS';
break;
case 2:
$val['clothes_size'] = 'S';
break;
case 3:
$val['clothes_size'] = 'M';
break;
case 4:
$val['clothes_size'] = 'L';
break;
case 5:
$val['clothes_size'] = 'XL';
break;
case 6:
$val['clothes_size'] = 'XXL';
break;
default:
$val['clothes_size'] = 'F';
break;
}
$objSheet->setCellValue('A' . $i, $val['user_id'])
->setCellValue('B' . $i, "4399XM-" . $val['empno'])
->setCellValue('C' . $i, $val['dept_id'])
->setCellValue('D' . $i, $val['job_id'])
->setCellValue('E' . $i, $val['clothes_style'])
->setCellValue('F' . $i, $val['clothes_size']);
$i++;
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xlsx"');
header("Content-Disposition:attachment;filename=$fileName.xlsx");
header('Cache-Control: max-age=0');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}