qwadmin中使用phpexcle

首先,把phpexcel放到ThinkPHP/Library/Vendor目录下

在controller中引用

require_once(ThinkPHP.'/Library/Vendor/PHPExcel.php');
$objPHPExcel = new \PHPExcel();

这里的PHPExcel()前面要加一个 \ 不然找不到对应方法

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

这里都要加 \

实例贴出

    /***************************************
    导出excel
     ***************************************/
    public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        require_once(ThinkPHP.'/Library/Vendor/PHPExcel.php');
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J');
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                if($j == 1 || $j == 4){
                    $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), "'".$expTableData[$i][$expCellName[$j][0]]);
                }else{
                    $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2),$expTableData[$i][$expCellName[$j][0]]);
                }

                //$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), "'".$expTableData[$i][$expCellName[$j][0]]);

                //$objPHPExcel->getActiveSheet(0)->getStyle('B')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);

            }
        }
        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;
    }
    /**
     *
     * 导出Excel
     */
    public function expUser(){
        $xlsName  = "User";
        $xlsCell  = array(
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名'),
            array('字段名','表头名')
        );
        $ZhangdanArr = session('ZhangdanArr');
        $xlsData  = $ZhangdanArr;
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

随机浏览