我们首先下载一个第三方的一个扩展包PHPExcel包,下载地址http://phpexcel.codeplex.com/

引入到到thinkPHP里的第三方扩展Library/Vendor/PHPExcel.

代码如下:

   公共方法:

/*导出日志的excel方法*/

 function exportExcel($expTitle,$expCellName,$expTableData){

$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称

$fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定

$fileName = iconv("utf-8", "gb2312", $fileName);

$cellNum = count($expCellName);

$dataNum = count($expTableData);

vendor('PHPExcel.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]]);

}

}

ob_end_clean();//清除缓冲区,避免乱码

header('pragma:public');

header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');

header("Content-Disposition:p_w_upload;filename=$fileName.xls");//p_w_upload新窗口打印inline本窗口打印

$arr = array_chunk($cellName,$cellNum,true);

foreach($arr[0] as $val){

    $objPHPExcel->getActiveSheet()->getColumnDimension($val)->setWidth(30);

  for($i=1;$i<=$dataNum+2;$i++){

  $a=$objPHPExcel->getActiveSheet()->getStyle($val.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

  }

}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit;

}


/**

     *

     * 执行导出Excel文件

     */

function doexportexcel(){//导出Excel

        $time = intval(I('times'));

        if(empty($time)){

              $this->error('请选择时间范围!');

           }

        switch ($time) {

           case 1:

               $mytime  = strtotime(date("Y-m-d H:i:s", strtotime("-3 month")));//三个月前的时间点

               $where['operate_time'] =array('gt',$mytime);

               break;

           case 2:

               $mytime  = strtotime(date("Y-m-d H:i:s", strtotime("-1 year")));//一年前的时间点

               $where['operate_time'] =array('gt',$mytime);

               break;

           default:

               $this->error('非法操作!');

               break;

        }

        $xlsName  = "日志管理";

        $xlsCell  = array(

        array('id','ID号'),

        array('description','行为名称'),

        array('operator','执行者'),

        array('operate_time','执行时间'),

        array('ip','执行IP'),

        array('url','操作url'),

        array('status','状态'),

        );

        $xlsModel = M('logs');

        $xlsData  = $xlsModel->Field('id,description,operator,operate_time,ip,url,status')->order('operate_time DESC')->where($where)->select();

        foreach ($xlsData as $k => $v)

        {

         $xlsData[$k]['status']=$v['status']==1?'失败':'成功';

         $xlsData[$k]['operate_time']=date('Y-m-d H:i:s',$v['operate_time']);

        }

        exportExcel($xlsName,$xlsCell,$xlsData);    

    }