码迷,mamicode.com
首页 > Web开发 > 详细

PHPExcel组件编程spl_autoload_register

时间:2019-06-02 23:04:46      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:defaults   pac   odi   def   导出数据   idt   buffer   cte   root   

E:\html\tproject\framework\modules\common\vendor\PHPExcel\Classes\PHPExcel.php

<?php

/** PHPExcel root directory */
if (!defined(‘PHPEXCEL_ROOT‘)) {
    define(‘PHPEXCEL_ROOT‘, dirname(__FILE__) . ‘/‘);
    require(PHPEXCEL_ROOT . ‘PHPExcel/Autoloader.php‘);
}

/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2015 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    ##VERSION##, ##DATE##
 */
class PHPExcel
{
    ......  
}

E:\html\tproject\framework\modules\common\vendor\PHPExcel\Classes\PHPExcel\Autoloader.php

<?php

PHPExcel_Autoloader::register();
//    As we always try to run the autoloader before anything else, we can use it to do a few
//        simple checks and initialisations
//PHPExcel_Shared_ZipStreamWrapper::register();
// check mbstring.func_overload
if (ini_get(‘mbstring.func_overload‘) & 2) {
    throw new PHPExcel_Exception(‘Multibyte function overloading in PHP must be disabled for string functions (2).‘);
}
PHPExcel_Shared_String::buildCharacterSets();

/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2015 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    ##VERSION##, ##DATE##
 */
class PHPExcel_Autoloader
{
    /**
     * Register the Autoloader with SPL
     *
     */
    public static function register()
    {
        if (function_exists(‘__autoload‘)) {
            // Register any existing autoloader function with SPL, so we don‘t get any clashes
            spl_autoload_register(‘__autoload‘);
        }
        // Register ourselves with SPL
        if (version_compare(PHP_VERSION, ‘5.3.0‘) >= 0) {
            return spl_autoload_register(array(‘PHPExcel_Autoloader‘, ‘load‘), true, true);
        } else {
            return spl_autoload_register(array(‘PHPExcel_Autoloader‘, ‘load‘));
        }
    }

    /**
     * Autoload a class identified by name
     *
     * @param    string    $pClassName        Name of the object to load
     */
    public static function load($pClassName)
    {
        if ((class_exists($pClassName, false)) || (strpos($pClassName, ‘PHPExcel‘) !== 0)) {
            // Either already loaded, or not a PHPExcel class request
            return false;
        }

        $pClassFilePath = PHPEXCEL_ROOT .
            str_replace(‘_‘, DIRECTORY_SEPARATOR, $pClassName) .
            ‘.php‘;

        if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
            // Can‘t load
            return false;
        }

        require($pClassFilePath);
    }
}

 使用

E:\html\tproject\framebota\platform\classes\Model\Export.php

    /**
     * Excel导出
     * param array $data  导出数据
     * param array $title 表格的字段名,可以改变 $length1和$length2来增长
     * return str  $filename 导出文件名 名字+导出日期
     */
    public function excel($data,$title,$filename){
        Kohana::load(Kohana::find_file(‘vendor‘,‘PHPExcel/Classes/PHPExcel‘));
        Kohana::load(Kohana::find_file(‘vendor‘,‘PHPExcel/Classes/PHPExcel/IOFactory‘));
        Kohana::load(Kohana::find_file(‘vendor‘,‘PHPExcel/Classes/PHPExcel/Reader/Excel5‘));
        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->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");
        $length1=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‘);
        $length2=array(‘A1‘,‘B1‘,‘C1‘,‘D1‘,‘E1‘,‘F1‘,‘G1‘,‘H1‘,‘I1‘,‘J1‘,‘K1‘,‘L1‘,‘M1‘,‘N1‘,‘O1‘,‘P1‘,‘Q1‘,‘R1‘,‘S1‘,‘T1‘
        ,‘U1‘,‘V1‘,‘W1‘,‘X1‘,‘Y1‘,‘Z1‘,‘AA1‘,‘AB1‘,‘AC1‘,‘AD1‘,‘AE1‘);
        //set width
        for($a=0;$a<count($title);$a++){
            $objPHPExcel->getActiveSheet()->getColumnDimension($length1[$a])->setWidth(20);
        }
        //set font size bold
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle($length2[0].‘:‘.$length2[count($title)-1])->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle($length2[0].‘:‘.$length2[count($title)-1])->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle($length2[0].‘:‘.$length2[count($title)-1])->getBorders()->getAllBorders()
            ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

        // set table header content
        for($a=0;$a<count($title);$a++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($length2[$a], $title[$a]);
        }
        for($i=0;$i<count($data);$i++){
            $buffer=$data[$i];
            $number=0;
            foreach ($buffer as $value) {
                $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($length1[$number].
                    ($i+2),$value,PHPExcel_Cell_DataType::TYPE_STRING);//设置单元格为文本格式
                $number++;
            }
            unset($value);
            $objPHPExcel->getActiveSheet()->getStyle($length1[0].($i+2).‘:‘.$length1[$number-1].
                ($i+2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle($length1[0].($i+2).‘:‘.$length1[$number-1].($i+2))->getBorders()
                ->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            $objPHPExcel->getActiveSheet()->getRowDimension($i+2)->setRowHeight(16);
        }
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        ob_end_clean();//清除缓冲区,避免乱码
        // Redirect output to a client’s web browser (Excel5)
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/download");
        header(‘Content-Disposition:attachment;filename="‘.$filename.date(‘Y-m-d‘,time()).‘.xls"‘);

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
        $objWriter->save(‘php://output‘);
    }

 备注: PHPExcel面向对象编程的思想待好好琢磨

PHPExcel组件编程spl_autoload_register

标签:defaults   pac   odi   def   导出数据   idt   buffer   cte   root   

原文地址:https://www.cnblogs.com/hnhycnlc888/p/10964889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!