thinkphp使用PHPExcel导出
thinkphp3.2 将PHPExcel下载包放在项目\ThinkPHP\Library\Vendor\PHPExcel
thinkphp5 将PHPExcel下载包放在项目\vendor\PHPExcel (调整后的PHPExcel在文末有下载链接) 在控制器中
use PHPExcel_IOFactory; use PHPExcel;
在方法中
public function exportData() { vendor("PHPExcel.PHPExcel.PHPExcel"); vendor("PHPExcel.PHPExcel.Writer.Excel5"); vendor("PHPExcel.PHPExcel.Writer.Excel2007"); vendor("PHPExcel.PHPExcel.IOFactory"); //导出条件 (以下获取数据的方法是thinkphp3.2,thinkphp5自行调整) $status = trim(I('get.status')); $starttime = trim(I('get.starttime')); $endtime = trim(I('get.endtime')); $map = array(); if($status != '' && $status != 0){ $map['status'] = $status; } if($starttime != '' && $endtime == ''){ $map['create_time'] = array('gt', strtotime($starttime.' 00:00:00')); } if($endtime != '' && $starttime == ''){ $map['create_time'] = array('lt', strtotime($endtime.' 23:59:59')); } if($starttime != '' && $endtime != '') { $map['create_time'] = array(array('gt', strtotime($starttime.' 00:00:00')), array('lt', strtotime($endtime.' 23:59:59'))); } $data = M('order')->where($map)->order('id desc')->select(); $title = '订单列表'.date('YmdHis',time()); //定义文件名 $objPHPExcel = new \PHPExcel(); // 表头 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '订单号') ->setCellValue('B1', '商品名称') ->setCellValue('C1', '支付金额') ->setCellValue('D1', '商品数量') ->setCellValue('E1', '手机号') ->setCellValue('F1', '创建时间') ->setCellValue('G1', '支付状态'); foreach($data as $k => $v){ if($v['status'] == 2){ $v['status'] = '已支付'; } else if($v['status'] == 1) { $v['status'] = '未支付'; } $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']); $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($k + 2), $v['order_sn']."\t"); //订单号太长无法正常显示,在后面加空格 $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($k + 2), $v['title']); $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($k + 2), $v['money']); $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($k + 2), $v['num']); $objPHPExcel->getActiveSheet(0)->setCellValue('E' . ($k + 2), $v['phone']); $objPHPExcel->getActiveSheet(0)->setCellValue('F' . ($k + 2), $v['create_time']); $objPHPExcel->getActiveSheet(0)->setCellValue('G' . ($k + 2), $v['status']); } //设置excel单元格的宽 $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(30); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(25); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10); $objPHPExcel->getActiveSheet()->setTitle($title); //设置sheet的名称 $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来 $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007"); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$title.'.xlsx"'); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件 exit; }
点击PHPExcel下载链接