//导出excel /**方法**/ public function exportExcel($expTitle,$expCellName,$expTableData){ $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称 $fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定 $fileName = iconv("utf-8", "gb2312", $fileName); $cellNum = count($expCellName); $dataNum = count($expTableData); vendor("PHPExcel"); $objPHPExcel = new PHPExcel(); $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'); $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格 // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s')); for($i=0;$i<$cellNum;$i++){ $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); } // Miscellaneous glyphs, UTF-8 for($i=0;$i<$dataNum;$i++){ for($j=0;$j<$cellNum;$j++){ $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]); } } header('pragma:public'); //Header('content-Type:application/vnd.ms-excel;charset=utf-8'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印 ob_clean(); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; } /** * * 导出Excel */ function expUser(){//导出Excel $xlsName = "User"; $xlsCell = array( array('id','ID'), array('name','名字'), array('username','工号'), array('logintime','最后登录时间'), array('phone','手机') ); $xlsModel = M('User'); $xlsData = $xlsModel->Field('id,name,username,logintime,phone')->select(); $this->exportExcel($xlsName,$xlsCell,$xlsData); }
<a href="{:U('User/expUser')}" > <input type="button" value="导出" class="btn btn-info active" style="padding-top: 2px;padding-bottom: 2px;margin-left:10px;"/> </a>
ps:http://www.thinkphp.cn/topic/14005.html
http://phpexcel.codeplex.com/releases/view/119187
http://www.thinkphp.cn/topic/22968.html