```php
use PHPExcel;
use PHPExcel_IOFactory;
public function export(){
$objExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objActSheet = $objExcel->getActiveSheet(0);
$objActSheet->setTitle('领取报表');//设置excel的标题
$objActSheet->setCellValue('A1', '编号');
$objActSheet->setCellValue('B1', '姓名');
$objActSheet->setCellValue('C1', '电话');
$objActSheet->setCellValue('D1', '区域');
$objActSheet->setCellValue('E1', '房型');
$objActSheet->setCellValue('F1', '面积');
$objActSheet->setCellValue('G1', '总房款');
$objActSheet->setCellValue('H1', '关注楼盘');
$objActSheet->setCellValue('I1', '选填楼盘');
$objActSheet->setCellValue('J1', '添加时间');
if (!empty($post['pstart_time']) and !empty($post['pend_time']) ) {
$where['create_time'] = array(
array('egt', strtotime($post['pstart_time'])),
array('lt',strtotime($post['pend_time']))
);
}
$data = empty($where) ? Db::name('send')->order('create_time desc')->select() : Db::name('send')->where($where)->order('create_time desc')->select();
$baseRow = 2; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
foreach ( $data as $r => $d ) {
$i = $baseRow + $r;
$objExcel->getActiveSheet()->setCellValue('A' . $i, $d['id']);
$objExcel->getActiveSheet()->setCellValue('B' . $i, $d['name']);
$objExcel->getActiveSheet()->setCellValue('C' . $i, $d['tel']);
$objExcel->getActiveSheet()->setCellValue('D' . $i, $d['sxregion_id']);
$objExcel->getActiveSheet()->setCellValue('E' . $i, $d['sxhouse_id'].$d['houseOther']);
$objExcel->getActiveSheet()->setCellValue('F' . $i, $d['sxarea_id']);
$objExcel->getActiveSheet()->setCellValue('G' . $i, $d['sxtotal_id']);
$objExcel->getActiveSheet()->setCellValue('H' . $i, $d['sxbuild_id']);
$objExcel->getActiveSheet()->setCellValue('I' . $i, $d['house1'].$d['house2'].$d['house3']);
$objExcel->getActiveSheet()->setCellValue('J' . $i, date("Y-m-d H:i:s",$d['create_time']));
}
$objExcel->setActiveSheetIndex(0);
//4、输出
$objExcel->setActiveSheetIndex();
header('Content-Type: applicationnd.ms-excel');
$time=date('Y-m-d');
header("Content-Disposition: attachment;filename=导出$time.xls");
header('Cache-Control: max-age=0');
header ('Pragma: public');
$objWriter->save('php://output');
}
太复杂了,参考下这个标准范例吧,不要直接输出,用文件接受
$objPHPExcel = new \PHPExcel();
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$titleExcelss = [
[
'Handle',
'Title',
'Body (HTML)',
'Vendor',
'Type',
'Tags',
'Published',
'Option1 Name',
'Option1 Value',
'Option2 Name',
'Option2 Value',
'Option3 Name',
'Option3 Value',
'Variant SKU',
'Variant Grams',
'Variant Inventory Tracker',
'Variant Inventory Qty',
'Variant Inventory Policy',
'Variant Fulfillment Service',
'Variant Price',
'Variant Compare At Price',
'Variant Requires Shipping',
'Variant Taxable',
'Variant Barcode',
'Image Src',
'Image Position',
'Image Alt Text',
'Gift Card',
'SEO Title',
'SEO Description',
'Google Shopping / Google Product Category',
'Google Shopping / Gender',
'Google Shopping / Age Group',
'Google Shopping / MPN',
'Google Shopping / AdWords Grouping',
'Google Shopping / AdWords Labels',
'Google Shopping / Condition',
'Google Shopping / Custom Product',
'Google Shopping / Custom Label 0',
'Google Shopping / Custom Label 1',
'Google Shopping / Custom Label 2',
'Google Shopping / Custom Label 3',
'Google Shopping / Custom Label 4',
'Variant Image',
'Variant Weight Unit',
'Variant Tax Code',
'Cost per item',
'Status'
]
];
$count = count($titleExcelss[0]);
$sheet->getStyle('A1:AV1')->getFont()->setSize(12);//字体大小
foreach ($titleExcelss as $ki => $node) {
$k = 'A';
foreach ($node as $one) {
$sheet->setCellValue($k . strval($ki + 1), $one);
#$sheet->getStyle('A1:AV1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居中
$sheet->getColumnDimension($k)->setWidth(25);#设置列的宽度
$k++;
}
}
$startRow = 1;
foreach ($new_list_array as $ki => $node) {
$startRow++;
$m = 'A';
for ($i = 0; $i < $count; $i++) {
$sheet->setCellValue($m . $startRow, $node[$i] ?? 0);
$m++;
}
}
$path = ROOT_PATH . 'goods' . rand(1, 3333333333333) . '.xls';
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($path);