1.在项目中创建一个与public同级的extend文件夹
2.下载一个PHPExcel插件放置到项目中的extend文件夹下(下载地址https://github.com/PHPOffice/PHPExcel)
3.调用下面的方法(数据库什么的自行设置)
public function out()
{
$path = dirname(__FILE__);
include_once(EXTEND_PATH . "phpexcel/PHPExcel.php");
include_once(EXTEND_PATH . "phpexcel/PHPExcel/Writer/Excel2007.php");
include_once(EXTEND_PATH . "phpexcel/PHPExcel/Writer/Excel5.php");
include_once(EXTEND_PATH . "phpexcel/PHPExcel/IOFactory.php");
$objPHPExcel = new \PHPExcel();
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$list = Income::with('user')->where('type',4)->select();
/*--------------设置表头信息------------------*/
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', '所属渠道')
->setCellValue('C1', '用户ID')
->setCellValue('D1', '微信昵称')
->setCellValue('E1', '手机号')
->setCellValue('F1', '支付宝帐号')
->setCellValue('G1', '真实姓名')
->setCellValue('H1', '金额')
->setCellValue('I1', '用户余额')
/*--------------开始从数据库提取信息插入Excel表中------------------*/
$i = 2;
foreach ($list as $v){
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $v['id']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $v['channel_id']);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $v['user_id']);
$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $v['user']['wx_nickname']);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $v['user']['mobile']);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $v['user']['alipay_account']);
$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $v['user']['alipay_name']);
$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $v['money']);
$objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $v['balance'])
$i++;
}
/*--------------下面是设置其他信息------------------*/
$fileName = date('YmdH_i_s',time()).'提现信息';
$objPHPExcel->getActiveSheet()->setTitle($fileName); //设置sheet的名称
$objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
$PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
header("Content-Disposition: attachment;filename=$fileName.xlsx");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save(EXTEND_PATH."../public/excel/".$fileName.'.xlsx'); //表示在$path路径下面生成demo.xlsx文件
$url = $this->request->domain().'/excel/'.$fileName.'.xlsx';
header('Location:'.$url);
die;
}