boxSpout
文档地址:https://opensource.box.com/spout/getting-started/
是目前php 最节省内存的 execl导入库.
使用迭代器的方式加载数据.
程序就是空间换时间,时间换空间的一个过程.
execl理论上是压缩的文件, 每一次读取都要解压,如果不缓存解压数据,那内存消耗低.
不bb上代码,本代码可直接复制使用, 前提是安装了库
phpoffice 导入1w数据的内存消耗 1w row 3 cell
Spout 导入一万的内存消耗 1w row 3 cell
public function import() { $file = $this->request->request('file'); if (!$file) { $this->error(__('Parameter %s can not be empty', 'file')); } $filePath = ROOT_PATH . DS . 'public' . DS . $file; $reader = ReaderEntityFactory::createReaderFromFile($filePath); $reader->open($filePath); if (!is_file($filePath)) { $this->error(__('No results were found')); } //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment'; $table = $this->model->getQuery()->getTable(); $database = thinkConfig::get('database.database'); $fieldArr = []; $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]); foreach ($list as $k => $v) { if ($importHeadType == 'comment') { $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; } else { $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME']; } } //加载文件 $insert = []; try { // 获取表迭代器 $iterator = $reader->getSheetIterator(); $iterator->rewind(); $sheet_field = $iterator->current(); // 获取行迭代器 $rowIter = $sheet_field->getRowIterator(); $rowIter->rewind(); // 获取首行 格子迭代器 $firstCell = $rowIter->current()->getCells(); $fields = []; foreach ($firstCell as $Cell) { $val = $Cell->getValue(); // 插入首字段array $fields[] = $val; } foreach ($rowIter as $key => $row) { $values = []; foreach ($row->getCells() as $Cell) { $val = $Cell->getValue(); // 获取数据 $values[] = is_null($val) ? '' : $val; } $row = []; /** * $fields 0 => 名称 * $values 0 => nekgod * $temp 名称 => nekgod */ $temp = array_combine($fields, $values); foreach ($temp as $k => $v) { if (isset($fieldArr[$k]) && $k !== '') { $row[$fieldArr[$k]] = $v; } } if ($row) { $insert[] = $row; } } unset($insert[0]); } catch (Exception $exception) { $this->error($exception->getMessage()); } if (!$insert) { $this->error(__('No rows were updated')); } try { //是否包含admin_id字段 $has_admin_id = false; foreach ($fieldArr as $name => $key) { if ($key == 'admin_id') { $has_admin_id = true; break; } } if ($has_admin_id) { $auth = Auth::instance(); foreach ($insert as &$val) { if (!isset($val['admin_id']) || empty($val['admin_id'])) { $val['admin_id'] = $auth->isLogin() ? $auth->id : 0; } } } $this->model->saveAll($insert); } catch (PDOException $exception) { $msg = $exception->getMessage(); if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) { $msg = "导入失败,包含【{$matches[1]}】的记录已存在"; }; $this->error($msg); } catch (Exception $e) { $this->error($e->getMessage()); } $this->success(); }
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。