标签:
首先去下载PHPExcel类,地址http://phpexcel.codeplex.com/
方法如下第一步引入这个扩展类
1 Vendor(‘PHPExcel‘);
第二部就是方法了,下面简单的实现方法
1 public function phpExcel() 2 { 3 // $list=array(array(‘dfcdf‘,‘测试‘,‘dfdfdsfd‘,‘dfd‘,‘dfdf‘),array(‘dfdsf‘,‘51策划书‘,‘dfdsfd‘,‘efed‘,‘dfdf‘)); 4 $list = session(‘cash_list‘); 5 $phpExcel = new \PHPExcel(); 6 $phpExcel->getProperties() 7 ->setCreator(‘ShuLaiBao‘)//创建人 8 ->setLastModifiedBy(‘ShuLaiBao‘)//最后修改人 9 ->setTitle(‘Office 2007 XLSX Test Document‘)//标题 10 ->setSubject(‘Office 2007 XLSX Test Document‘)//题目 11 ->setDescription(‘Test document for Office 2007 XLSX, generated using PHP classes.‘)//描述 12 ->setKeywords(‘office 2007 openxml php‘) 13 ->setCategory(‘Test Result file‘); 14 //设置当前的sheet 15 $phpExcel->setActiveSheetIndex(0); 16 //设置表头 17 $phpExcel->getActiveSheet() 18 ->setCellValue(‘A1‘, ‘用户‘) 19 ->setCellValue(‘B1‘, ‘微信号‘) 20 ->setCellValue(‘C1‘, ‘商户订单号‘) 21 ->setCellValue(‘D1‘, ‘付款订单号‘) 22 ->setCellValue(‘E1‘, ‘发送时间‘); 23 foreach ($list as $key => $value) { 24 $phpExcel->getActiveSheet() 25 ->setCellValue(‘A‘ . ($key + 2), $value[‘user_login‘]) 26 ->setCellValue(‘B‘ . ($key + 2), $value[‘re_user_name‘]) 27 ->setCellValue(‘C‘ . ($key + 2), $value[‘mcn_billno‘]) 28 ->setCellValue(‘D‘ . ($key + 2), $value[‘payment_no‘]) 29 ->setCellValue(‘E‘ . ($key + 2), date(‘Y-m-d H:i:s‘, $value[‘send_time‘])); 30 } 31 // 设置sheet的标题 32 $phpExcel->getActiveSheet()->setTitle(‘发送记录‘); 33 // 创建文件格式写入对象实例, uncomment 34 //$objWriter = PHPExcel_IOFactory::createWriter($phpExcel,‘Excel5‘); 35 //$objWriter = new \PHPExcel_Writer_Excel5($phpExcel); // 用于其他版本格式 36 //$objWriter = new PHPExcel_Writer_Excel2007($objExcel); // 用于 2007 格式 37 $objWriter = new \PHPExcel_Writer_Excel5($phpExcel); 38 //设置文件名 39 $filename = date(‘Y-m-d H:i:s‘, time()) . ‘_微信红包发送记录.xls‘; 40 //设置header 41 // ob_clean(); 42 // flush(); 43 header("Content-Type: application/force-download"); 44 header("Content-Type: application/octet-stream"); 45 header("Content-Type: application/download"); 46 header(‘Content-Disposition:inline;filename="‘ . $filename . ‘"‘); 47 header("Content-Transfer-Encoding: binary"); 48 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 49 header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 50 header("Pragma: no-cache"); 51 $objWriter->save(‘php://output‘); 52 die();//终止执行 53 }
标签:
原文地址:http://www.cnblogs.com/dreamysky/p/5914823.html