<?php
// +----------------------------------------------------------------------
// | WANCMS游戏平台管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.wancms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 郑建斌
// +----------------------------------------------------------------------
// | 修改时间: 2015/12/29
// +----------------------------------------------------------------------

namespace Asset\Controller;
use Common\Controller\AdminbaseController;
//excel 导出控制器

 class ExcelController extends AdminbaseController{
    public function exportExcel($expTitle,$expCellName,$expTableData){
//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能import导入
   import("Org.Util.PHPExcel");
   import("Org.Util.PHPExcel.Writer.Excel5");
   import("Org.Util.PHPExcel.IOFactory.php");
   //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();
        $objPHPExcel->getProperties()->setCreator("Da")
            ->setLastModifiedBy("Da")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX,generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");      
        $objPHPExcel->getActiveSheet(0)->setTitle($expTitle);//标题
        $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);//单元格宽度
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Arial');//设置字体
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);//设置字体大小
        $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');
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
         for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]); 
        } 
       for($i=0;$i<$dataNum;$i++){
          for($j=0;$j<$cellNum;$j++){
            $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
          }             
        }  
        $expTitle=$expTitle.date("Ymd");
        header('Content-Type: application/vnd.ms-excel');
        //header('Content-Disposition: attachment;filename="$expTitle'.date("YmdHis").'.xls"');
        header("Content-Disposition:attachment;filename=$expTitle.xls");
        header('Cache-Control: max-age=0');
        ob_clean();//关键
        flush();//关键
        $objWriter =  \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit; 
         }
 }