Jeen 2016-10-18 13:12:34 6713次浏览 4条评论 8 2 0

不想装过多的扩展依赖?想添加一个phpexcel进行测试,不需要的时候可以随意删除?

那么或许这个就是你想要的 😄

如果你用的是Yii2 advance模板 建议在common中创建并集成,如果是basic模板 就随意了

首先,搜索并下载phpexcel源码,这个就不赘述了。

源码不需要做任何修改,创建一个自定义的扩展类,如下:

<?php
namespace common\extend\phpexcel; //your class namespace

require 'PHPExcel.php';//path to file PHPExcel.php

//这边是我集成使用的类名  可以改为任意你想要的
class JExcel extends \PHPExcel
{
    public static $instance;
    public static function getInstance()
    {
        if (empty(self::$instance)) self::$instance = new self();
        return self::$instance;
    }
    public static function init() {
        return true;
    }
 
    /**
     * 获取Excel表单数据
     * @param int $inFile  读取文件路径
     * @param bool $index   读取表格索引,默认读取所有数据 合并后返回
     * @return array
     */
    public function readSheet($inFile,$index = false)
    {
        $type = \PHPExcel_IOFactory::identify($inFile);
        $reader = \PHPExcel_IOFactory::createReader($type);
        $sheet = $reader->load($inFile);
//        $aIndex = $sheet->getActiveSheetIndex();//获取当前活动表格索引
        $sCount = $sheet->getSheetCount();//获取文件中表格数量
//        Jeen::echoln($aIndex.' of '.$sCount);
        if(is_int($index) && $index < $sCount && $index >= 0)
            return $sheet->getSheet($index)->toArray();
        if($sCount == 1)
            return $sheet->getSheet(0)->toArray();
        $data = [];
        for ($i=0;$i<$sCount;$i++) {
            $data[] = $sheet->getSheet($i)->toArray();
        }
        unset($sheet); unset($reader); unset($type);
        return $data;
    }

    /**
     * 将数据保存至Excel 表格
     * @param string $outFile 输出文件路径
     * @param array $data  需要保存的数据  二维数组
     * @return bool
     */
    public function saveSheet($outFile,array $data)
    {
        $path = explode('/',$outFile);
        unset($path[count($path)-1]);
        $path = implode('/',$path) . DS;
        if(!file_exists($path)) { //目录不存在 则创建目录 并开放权限
            @mkdir($path, 0777, TRUE); @chmod($path, 0777);
        }
        $newExcel = new \PHPExcel();
        $newSheet = $newExcel->getActiveSheet();
        $newSheet->fromArray($data);
        $objWriter = \PHPExcel_IOFactory::createWriter($newExcel, 'Excel5');
        $objWriter->save($outFile);
        unset($objWriter); unset($newSheet); unset($newExcel);
        return true;
    }

    /**
     * @param array $data 需要过滤处理的数据 二维数组
     * @param int $cols  取N列
     * @param int $offset  排除 N 行
     * @param bool|int $must 某列不可为空  0 - index
     * @return array
     */
    public function handleSheetArray(array $data,$cols = 10,$offset = 1,$must = false)
    {
        $final = [];
        if($must && $must >= $cols) $must=false;
        foreach($data as $k=>$row)
        {
            if($k < $offset) continue;
            $t = [];
            for($i=0;$i<$cols;$i++) {
                if(isset($row[$i])) $t[$i] = trim(strval($row[$i]));
                else $t[$i] = '';
            }
            if(is_array($row) && implode('',$t) && ($must===false || $t[$must])) {
                $final[] = $t; continue;
            }
        }
        return $final;
    }

}

使用范例
JExcel::getInstance()->readSheet('/path/to/your/excel.xls')

类中包含了 读取 存储 以及预处理的方法,如果你不需要可以直接删除 😆

觉得很赞
  • 评论于 2016-11-01 17:03 举报

    如果读取的 列数 超过 Z 会有问题的。

    2 条回复
    评论于 2016-11-03 15:40 回复

    抱歉,针对特定数据量的没有实测,日常需求除了内存限制,其他基本没碰到过大问题。 欢迎提供优化建议和方法 😆

    评论于 2016-11-03 16:32 回复

    如果读取太大的话 还是用 csv文件把。内存占用 少了太多了。

  • 评论于 2016-11-02 09:35 举报

    这个好,赞一个。

    1 条回复
    评论于 2016-11-03 15:42 回复

    gxun ? 😄 谢谢支持

  • 评论于 2017-01-04 17:00 举报

    66666666666666

  • 评论于 2017-03-22 11:23 举报

    非常感谢。不想使用composer安装phpexcel,你这个方案正好合适。

您需要登录后才可以评论。登录 | 立即注册