// 增加内存, 不限执行时间 ini_set("memory_limit", "1024M");
set_time_limit(0);
执行环境: php7
$fileds = [
'id' => 'ID',
'wx_openid' => '微信openid',
'wx_name' => '微信昵称',
'award_type' => '抽奖类型',
'award_id' => '奖品ID',
'award_name' => '奖品名称',
'award_num' => '奖品数量',
'award_time' => '获奖时间',
'lbs' => '坐标(经度,纬度)',
'lbs_province' => '坐标省份',
'lbs_city' => '坐标城市',
'lbs_address' => '坐标地址',
];
$awardTypes = DAward::getsAwardTypes();
foreach($rows as $k => $row) {
$row['award_type'] = $awardTypes[$row['award_type']]['desc'];
$row['award_time'] = date('Y-m-d H:i:s', $row['award_time']);
$row['lbs'] = $row['lbs_lon'] . ',' . $row['lbs_lat'];
$row['wx_name'] = str_replace(['^', '='], '', $this->filterEmoji($row['wx_name']));
$rows[$k] = $row;
}
$fileName = date('Ymd', time()) . '抽奖记录.xlsx';
$sheetName = '抽奖记录';
$this->outputExcel($fileds, $rows, $fileName, $sheetName);
protected function outputExcel($fileds, $allDatas, $fileName, $sheetName)
{ // 数据量大时,导出失败, 压缩一下,减少内存使用
include KIF_PATH . '/PHPExcel/Settings.php';
if (!\PHPExcel_Settings::setCacheStorageMethod('MemoryGZip')) {
die('MemoryGZip' . " 缓存方法不可用" . EOL);
}
$phpexcel = new \PHPExcel();
$letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'];
// 设置表头
$index = 0;
foreach ($fileds as $field => $fieldName) {
$letter = $letters[$index];
$phpexcel->setActiveSheetIndex(0)->setCellValue($letter . '1', $fieldName);
$index++;
}
$phpexcel->createSheet();
// 标签名
$phpexcel->getActiveSheet()->setTitle($sheetName);
// 使用第一个表
$phpexcel->setActiveSheetIndex(0);
$key = 2;
foreach ($allDatas as $row) {
$i = $key;
$index = 0;
foreach ($fileds as $field => $fieldName) {
$letter = $letters[$index];
$phpexcel->getActiveSheet()->setCellValue($letter . $i, $row[$field]);
$index++;
}
$key++;
}
ob_end_clean();
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$objWriter = \PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
$objWriter->save('php://output');
}
|
请发表评论