Yii2 手动快速集成 phpexcel [ 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')
类中包含了 读取 存储 以及预处理的方法,如果你不需要可以直接删除
Jeen
注册时间:2014-10-03
最后登录:2024-12-25
在线时长:18小时17分
最后登录:2024-12-25
在线时长:18小时17分
- 粉丝9
- 金钱1120
- 威望190
- 积分3200
热门源码
- 基于 Yii 2 + Bootstrap 3 搭建一套后台管理系统 CMF
- 整合完 yii2-rbac+yii2-admin+adminlte 等库的基础开发后台源码
- 适合初学者学习的一款通用的管理后台
- yii-goaop - 将 goaop 集成到 Yii,在 Yii 中优雅的面向切面编程
- yii-log-target - 监控系统异常且多渠道发送异常信息通知
- 店滴云1.3.0
- 面向对象的一小步:添加 ActiveRecord 的 Scope 功能
- Yii2 开源商城 FecShop
- 基于 Yii2 开发的多店铺商城系统,免费开源 + 适合二开
- leadshop - 基于 Yii2 开发的一款免费开源且支持商业使用的商城管理系统
共 4 条评论
如果读取的 列数 超过 Z 会有问题的。
抱歉,针对特定数据量的没有实测,日常需求除了内存限制,其他基本没碰到过大问题。 欢迎提供优化建议和方法
如果读取太大的话 还是用 csv文件把。内存占用 少了太多了。
这个好,赞一个。
gxun ?
谢谢支持
66666666666666
非常感谢。不想使用composer安装phpexcel,你这个方案正好合适。