zoukankan      html  css  js  c++  java
  • thinkphp 3.2与phpexcel

    thinkphp版本:3.2

    1.在http://phpexcel.codeplex.com/下载最新PHPExcel

    2.把Classes目录下的文件(PHPExcel.php和PHPExcel文件夹)放到ThinkPHPLibraryOrgUtil目录下;并且把PHPExcel.phpg改名为PHPExcel.class.php

    3.导出数据

    第一种方案【单一完整版】:

    public function ff(){
            //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
            import("Org.Util.PHPExcel");
            
            
            //创建PHPExcel对象,注意,不能少了
            $objPHPExcel = new PHPExcel();
            $objProps = $objPHPExcel->getProperties();
            
            
            
            //设置表头
            $key = ord("A");
            foreach($headArr as $v){
                $colum = chr($key);
                $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
                $key += 1;
            }
            
            $column = 2;
            $objActSheet = $objPHPExcel->getActiveSheet();
            
            $date = date("Y_m_d",time());
            $fileName .= "_{$date}.xls";
            
            $data=array(
                array('username'=>'wangwu','password'=>"http://www.thinkphp.cn/Public/new/img/header_logo.png"),
                );
                
            foreach($data as $key => $rows){ //行写入
                $span = ord("A");
                foreach($rows as $keyName=>$value){// 列写入
                    $j = chr($span);
                    $objActSheet->setCellValue($j.$column, $value);
                    $span++;
                }
                $column++;
        }
    
            $fileName = iconv("utf-8", "gb2312", $fileName);
            //重命名表
            // $objPHPExcel->getActiveSheet()->setTitle('test');
            //设置活动单指数到第一个表,所以Excel打开这是第一个表
            $objPHPExcel->setActiveSheetIndex(0);
            header('Content-Type: application/vnd.ms-excel');
            header("Content-Disposition: attachment;filename="$fileName"");
            header('Cache-Control: max-age=0');
    
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output'); //文件通过浏览器下载
            
            
        }

    第二种方案【分开完整版】:

    public function out(){
            $data=array(
                array('username'=>'zhangsan','password'=>"123456"),
                array('username'=>'lisi','password'=>"abcdefg"),
                array('username'=>'wangwu','password'=>"111111"),
                );
            //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
            import("Org.Util.PHPExcel");
            import("Org.Util.PHPExcel.Writer.Excel5");
            import("Org.Util.PHPExcel.IOFactory.php");
    
            $filename="test_excel";
            $headArr=array("用户名","密码");
            $this->getExcel($filename,$headArr,$data);
        }
    
        private    function getExcel($fileName,$headArr,$data){
                //对数据进行检验
                if(empty($data) || !is_array($data)){
                    die("data must be a array");
                }
                //检查文件名
                if(empty($fileName)){
                    exit;
                }
    
                $date = date("Y_m_d",time());
                $fileName .= "_{$date}.xls";
    
                //创建PHPExcel对象,注意,不能少了
                $objPHPExcel = new PHPExcel();
                $objProps = $objPHPExcel->getProperties();
                
                //设置表头
                $key = ord("A");
                foreach($headArr as $v){
                    $colum = chr($key);
                    $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
                    $key += 1;
                }
                
                $column = 2;
                $objActSheet = $objPHPExcel->getActiveSheet();
                foreach($data as $key => $rows){ //行写入
                    $span = ord("A");
                    foreach($rows as $keyName=>$value){// 列写入
                        $j = chr($span);
                        $objActSheet->setCellValue($j.$column, $value);
                        $span++;
                    }
                    $column++;
            }
    
                $fileName = iconv("utf-8", "gb2312", $fileName);
                //重命名表
                   // $objPHPExcel->getActiveSheet()->setTitle('test');
                //设置活动单指数到第一个表,所以Excel打开这是第一个表
                $objPHPExcel->setActiveSheetIndex(0);
                header('Content-Type: application/vnd.ms-excel');
                header("Content-Disposition: attachment;filename="$fileName"");
                header('Cache-Control: max-age=0');
    
                  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                $objWriter->save('php://output'); //文件通过浏览器下载
                exit;
            }
  • 相关阅读:
    Spring 增强类型
    Spring IOC及Bean的生命周期
    Spring
    Mybatis注解
    MyBatis关联查询
    LoadRunner(1)
    Selenium(6)
    Selenium(5)
    Selenium(4)
    Selenium(3)
  • 原文地址:https://www.cnblogs.com/wesky/p/5620547.html
Copyright © 2011-2022 走看看