Thinkphp实现excel数据的导入导出

2021-07-02 12:03

阅读:603

标签:icon   func   根据   set   tac   isp   name   注意   斜杠   

 在http://phpexcel.codeplex.com/下载最新PHPExcel放到Vendor下,注意位置:ThinkPHP\Extend\Vendor\PHPExcel\PHPExcel.php。

 这几句可能需要修改:

//有的需要加反斜杠
$objPHPExcel
= new \PHPExcel(); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);

实现代码:

 

public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv(‘utf-8‘, ‘gb2312‘, $expTitle);//文件名称
        $fileName = ‘充值金额明细表‘.date(‘_YmdHis‘);//or $xlsTitle 文件名称可根据自己情况设定$_SESSION[‘username‘]
        $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]].‘ ‘);//如果导出订单号、身份证号等,可能会因为数字太长,导出的excel会显示科学技术方式显示,可以加空格‘ ‘来避免
          }             
        }  
        
        header(‘pragma:public‘);
        header(‘Content-type:application/vnd.ms-excel;charset=utf-8;name="‘.$xlsTitle.‘.xls"‘);
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);  
        $objWriter->save(‘php://output‘); 
        exit;   
    }

    public function excel(){
        //$months = I(‘get.months‘);
        //$month_start = strtotime($months);//获取指定月份月初时间戳  
        //$month_end   = mktime(23, 59, 59, date(‘m‘, strtotime($months))+1, 00);//指定月份月末时间戳

        //$where[‘type‘] = 2;//群主充值
        //$where[‘time‘] = array(array(‘gt‘,$month_start),array(‘lt‘,$month_end));
        //$m = M(‘dap_stream‘);
        // $list = $m->where($where)->field(‘id,stream_id,time,player_id,stream_money‘)->select();

        $xlsName  = "User";
        $xlsCell  = array(
        array(‘id‘,‘ID‘),
        array(‘stream_id‘,‘订单号‘),
        array(‘time‘,‘交易时间‘),
        array(‘player_id‘,‘群主账号‘),
        array(‘stream_money‘,‘充值金额‘)  
        );
        $xlsData  = $m->where($where)->field("id,stream_id, FROM_UNIXTIME(time, ‘%Y-%m-%d %H:%i:%S‘) time,player_id,stream_money")->select();    
            // dump($xlsData);exit();
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

 

Thinkphp实现excel数据的导入导出

标签:icon   func   根据   set   tac   isp   name   注意   斜杠   

原文地址:http://www.cnblogs.com/meetuj/p/7127945.html


评论


亲,登录后才可以留言!