$mModel= new Model();
$list = $mModel->getList();
$objectPHPExcel = new \PHPExcel();
$objectPHPExcel->setActiveSheetIndex(0);
//报表头的输出
$objectPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'ID');
$objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '用户id');
$objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '用户昵称');
$objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '留言');
$objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '留言时间');
$n = 2;
foreach ($list as $key => $value) {
$objectPHPExcel->getActiveSheet()->setCellValue('A' . ($n), $value->id);
$objectPHPExcel->getActiveSheet()->setCellValue('B' . ($n), $value->uid);
$objectPHPExcel->getActiveSheet()->setCellValue('C' . ($n), $value->relationWechatUser->nickname);
$objectPHPExcel->getActiveSheet()->setCellValue('D' . ($n), $value->message);
$objectPHPExcel->getActiveSheet()->setCellValue('E' . ($n), date('Y-m-d H:i:s', $value->create_time));
$n ++;
}
ob_end_clean();
ob_start();
header('Content-Type : application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="' . '用户留言-' . date("Y年m月j日") . '.xls"');
$objWriter= \PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel5');
$objWriter->save('php://output');
//$objWriter->save(Yii::$app->basePath . '/web/export_txt.xlsx');
//放在服务器上