TP6 EXECL 导入

56 阅读1分钟

namespace app\admin\controller;

use app\admin\model\TMatching as TMatchingModel;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

use PhpOffice\PhpSpreadsheet\IOFactory;

use PhpOffice\PhpSpreadsheet\Reader\Xls;

use think\Exception;

use think\facade\Db;

class TMatching extends Base { set_time_limit(0);

ini_set('memory_limit', '252M'); // 开启事务 Db::startTrans(); try {

request->post();","marks":[]}]}],"state":{}}]'> sheet=0;//指定获取工作表sheet = 0; //指定获取工作表 options = [];

file=file = this->request->file('file');

params=params = this->request->post();

f (!$file) {

throw new Exception("文件出错!");

} suffix=suffix = file->getOriginalExtension();

if (!in_array($suffix, ['xlsx', 'xls'])) {

throw new Exception("文件只允许上传 ExEcl 表格文件!");

}

filepath=file_path = file->getPathName();

$reader = IOFactory::createReader('Xlsx');

if (!reader>canRead(reader->canRead(file_path)) {

/** @var Xls $objRead */

$reader = IOFactory::createReader('Xls');

}

if (!empty(options) && !reader->setReadDataOnly(true)) {

throw new Exception("获取文件数据失败!");

}

obj=obj = reader->load($file_path);

currSheet=currSheet = obj->getSheet($sheet);

/* 获取总行数 - 文件底部多余制表行*/

rowTotal=rowTotal = currSheet->getHighestRow();

if ($rowTotal > 5000) {

throw new Exception("文件条数超过上传限制,请分割文件后重新尝试上传!");

}

if ($rowTotal <= 0) {

throw new Exception("文件数据不能为空,请重新上传!");

}

/* 获取总列数 */

highestColumn=highestColumn = currSheet->getHighestColumn();

highestColumnIndex=Coordinate::columnIndexFromString(highestColumnIndex = Coordinate::columnIndexFromString(highestColumn);

if ($highestColumnIndex >= 40) {

throw new Exception("文件列数超过可上传范围,请分割文件后重试!");

}

headings=headings = currSheet->rangeToArray('A1:' . $highestColumn . 1, NULL, TRUE, FALSE);

//过滤表头数组 //验证文件表头是否符合条件

checkHead=checkHead = this->checkHeader($headings);

if ($checkHead != '') {

throw new Exception($checkHead);

}

$rowInfo = [];

for (row=2;row = 2; row <= rowTotal;rowTotal; row++) {

// info=info = currSheet->getCellByColumnAndRow(1, $row)->getValue();

rowData=rowData = currSheet->rangeToArray('A' . row.:.row . ':' . highestColumn . $row, NULL, TRUE, FALSE);

// rowInfo[rowInfo[row] = array_combine(headings[0],headings[0], rowData[0]);

if (array_combine(headings[0],headings[0], rowData[0]) == false) {

continue;

} else {

rowInfo[rowInfo[row] = array_combine(headings[0],headings[0], rowData[0]);

}

}

///------------------------------

unset($rowData);

}

}