如何生成随机数激活码,以及如何将数据库中激活码导出至本地Excel文件

public function getRandColor() {
   $chars = array(
            “A”, “B”, “C”, “D”, “E”, “F”,”G”,”H”,”J”,”K”,”M”,”N”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”,”1″, “2”,
            “3”, “4”, “5”, “6”, “7”, “8”, “9”
        );
        $charsLen = count($chars) – 1;
        // 将数组打乱
        shuffle($chars);
        $output = “”;
        for ($i = 0; $i < 12; $i++) {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }
public function getRandNum() {
   $chars = array(
            “0”,”1″, “2”,”3″, “4”, “5”, “6”, “7”, “8”, “9”,”10″
        );
        $charsLen = count($chars) – 1;
        // 将数组打乱
        shuffle($chars);
        $output = “”;
        for ($i = 0; $i < 1; $i++) {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }
    public function getcode(){
        $num=$_POST[‘num’];
     $data=””;
     for($n = 0; $n < $num; $n++){
        $data.=”(‘”.$this->getRandColor().”‘,'”.$this->getRandNum().”‘,'”.time().”‘),”;
     }
        $data = rtrim($data, ‘,’);
        $res= new \Admin\Model\Activation_codeModel();
        $result=$res->ins($data);
        if(!empty($result)){
        echo json_encode(array(‘message’=>$data));
        }else{
        echo json_encode(array(‘message’=>’wrong’));
        }
    }
    public function intoexcel(){
     $dir = “C:\Users\Administrator\Desktop”;   //设置路径
        Vendor(‘PHPExcel’);   //引入文件
        $objPHPExcel = new PHPExcel();  //实例化PHPExcel类,等同于在桌面上新建一个excel表格
        $objSheet = $objPHPExcel->getActiveSheet();    //获得当前活动sheet的操作对象
        $objSheet->setTitle(‘demo’);    //给当前活动sheet设置名称
        $data[]=array();
        $res=M(‘activation_code’)->select();
        $result=array_unshift($res,array(‘id’=>’编号’,’activation_code’=>’激活码’,’point’=>’积分’,’states’=>’状态’,’time’=>’时间’));
        foreach($res as $k =>$v){
    if($k>=1){
    if($res[$k][‘states’]==0){
        $res[$k][‘states’]=”未使用”;
    $res[$k][‘times’]=date(“Y-m-d H:i:s”,$v[“times”]);
    }else{
    $res[$k][‘states’]=”使用中”;
    $res[$k][‘times’]=date(“Y-m-d H:i:s”,$v[“times”]);
    }
    }
        }
$objSheet->fromArray($res);   //直接加载数据块来填充数据
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,”Excel2007″);   //按照指定格式生成excel文件
$success=$objWriter->save($dir.’/activation.xlsx’);
echo json_encode(array(‘message’=>’生成成功,请在桌面查看,文件名称:activation.xlsx’));
}