136 lines
4.0 KiB
PHP
136 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\app\exam;
|
|
|
|
use app\common\controller\Backend;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
/**
|
|
* 文件导入
|
|
*
|
|
* @icon fa fa-circle-o
|
|
*/
|
|
class WarehouseImport extends Backend
|
|
{
|
|
|
|
/**
|
|
* AttachmentGroup模型对象
|
|
* @var \app\admin\model\attachment\AttachmentGroup
|
|
*/
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->warehouseGroupModel = new \app\admin\model\app\exam\WarehouseGroup;
|
|
$this->warehouseQuestionModel = new \app\admin\model\app\exam\WarehouseQuestion;
|
|
$this->warehouseImportModel = new \app\admin\model\app\exam\WarehouseImport;
|
|
}
|
|
|
|
public function handle(){
|
|
|
|
$file = $this->request->file('file');
|
|
$groupId = input('group_id');
|
|
|
|
$type = 'excel';
|
|
|
|
|
|
if (empty($file)) {
|
|
$this->error("请上传文件");
|
|
}
|
|
|
|
$fileInfo = $file->getInfo();
|
|
|
|
$fileTypes = [
|
|
'word'=>['application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword'],
|
|
'excel'=>['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel'],
|
|
];
|
|
|
|
$suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
|
|
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
|
|
|
|
if (!in_array($fileInfo['type'], $fileTypes[$type]) || in_array($suffix, ['php', 'html', 'htm', 'phar', 'phtml']) || preg_match("/^php(.*)/i", $suffix)) {
|
|
$this->error("文件类型不符合要求");
|
|
}
|
|
|
|
|
|
//获取文件类型
|
|
|
|
$size = $fileInfo['size']/1024/1024;
|
|
|
|
if($size>10){
|
|
$this->error("文件体积过大");
|
|
}
|
|
|
|
//可以处理文件了 图片临时地址
|
|
$fileTempPath = $file->getRealPath();
|
|
|
|
$reader = new Xlsx();
|
|
|
|
//加载文件
|
|
|
|
if (!$PHPExcel = $reader->load($fileTempPath)) {
|
|
$this->error(__('Unknown data format'));
|
|
}
|
|
|
|
|
|
|
|
$count = 0;
|
|
for($i = 0 ;$i < $PHPExcel->getSheetCount() ;$i++){
|
|
$sheetData = $this->getSheetData($PHPExcel,$i,$groupId);
|
|
$count += count($sheetData);
|
|
foreach (array_chunk($sheetData,100) as $data){
|
|
$this->warehouseQuestionModel->insertAll($data);
|
|
}
|
|
}
|
|
|
|
$this->success("已导入{$count}条题目");
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取工作表的数据
|
|
* @param $PHPExcel
|
|
* @param $typeIndex
|
|
* @param int $groupId 分组ID
|
|
* @return array
|
|
*/
|
|
public function getSheetData($PHPExcel,$typeIndex,$groupId = 0){
|
|
|
|
$insert = [];
|
|
$currentSheet = $PHPExcel->getSheet($typeIndex); //读取文件中的第一个工作表
|
|
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
|
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
|
|
|
|
|
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
$values = [];
|
|
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
$values[] = is_null($val) ? '' : $val;
|
|
}
|
|
$rowData = $this->warehouseImportModel->getRowData($values,$typeIndex,$groupId);
|
|
|
|
if($rowData){
|
|
$rowData['uniacid'] = UNIACID;
|
|
$rowData['group_id'] = $groupId;
|
|
$rowData['createtime'] = time();
|
|
|
|
|
|
foreach ($rowData as $key => $val){
|
|
if($val instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText){
|
|
$rowData[$key] = $val->getPlainText();
|
|
}
|
|
}
|
|
|
|
$insert[] = $rowData;
|
|
}
|
|
}
|
|
|
|
return $insert;
|
|
}
|
|
|
|
|
|
} |