作者 曹东东

1

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\school\controller\match; 3 namespace app\school\controller\match;
4 4
  5 +use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
5 use think\Db; 6 use think\Db;
6 use app\common\controller\ManystoreBase; 7 use app\common\controller\ManystoreBase;
7 use app\school\model\match\MatchItems; 8 use app\school\model\match\MatchItems;
@@ -147,9 +148,24 @@ class MatchItemPrize extends ManystoreBase @@ -147,9 +148,24 @@ class MatchItemPrize extends ManystoreBase
147 148
148 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); 149 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
149 $reader->setReadDataOnly(true); 150 $reader->setReadDataOnly(true);
150 - $spreadsheet = $reader->load($file['save_path']); //载入excel表格 151 + $PHPExcel = $reader->load($file['save_path']); //载入excel表格
151 152
152 - $datas = $spreadsheet->getActiveSheet()->toArray(); 153 + $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  154 + $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  155 + $currentSheetName = $PHPExcel->getSheetNames(); //读取文件中的第一个工作表
  156 + $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  157 + $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  158 + $datas = [];
  159 + for ($currentRow = 3; $currentRow <= $allRow; $currentRow++) {
  160 + for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  161 + $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  162 + if(!$val){
  163 + //删除空行
  164 + continue;
  165 + }
  166 + $datas[$currentRow][] = $val;
  167 + }
  168 + }
153 unlink($_SERVER['DOCUMENT_ROOT'] . "/assets/file/".$tmpName); 169 unlink($_SERVER['DOCUMENT_ROOT'] . "/assets/file/".$tmpName);
154 if(empty($datas)) { 170 if(empty($datas)) {
155 $this->error('更新失败'); 171 $this->error('更新失败');
@@ -157,19 +173,12 @@ class MatchItemPrize extends ManystoreBase @@ -157,19 +173,12 @@ class MatchItemPrize extends ManystoreBase
157 $newArr = []; 173 $newArr = [];
158 $existsArr = []; 174 $existsArr = [];
159 foreach ($datas as $key => &$val) { 175 foreach ($datas as $key => &$val) {
160 - if($key <= 1) {  
161 - unset($datas[$key]);  
162 - continue;  
163 - }  
164 $addIds[] = $val[0]; 176 $addIds[] = $val[0];
165 - $val[5] = $date = date('Y-m-d', ($val[5] - 25569) * 24 * 3600); 177 + $val[5] = date('Y-m-d', ($val[5] - 25569) * 24 * 3600);
166 } 178 }
167 $exists = $this->model->whereIn('match_items_id',$addIds)->select(); 179 $exists = $this->model->whereIn('match_items_id',$addIds)->select();
168 foreach ($datas as $vval) { 180 foreach ($datas as $vval) {
169 $tmp['match_items_id'] = $vval[0]; //id 181 $tmp['match_items_id'] = $vval[0]; //id
170 - if(empty($vval[4]) || empty($vval[5])) {  
171 - $this->error('导入文件有误,请重新导入');  
172 - }  
173 $tmp['prize_type'] = $vval[4]; //奖项类型 182 $tmp['prize_type'] = $vval[4]; //奖项类型
174 $tmp['get_time'] = $vval[5]; //获奖时间 183 $tmp['get_time'] = $vval[5]; //获奖时间
175 if(!empty($vval[6])) { 184 if(!empty($vval[6])) {
@@ -195,7 +204,7 @@ class MatchItemPrize extends ManystoreBase @@ -195,7 +204,7 @@ class MatchItemPrize extends ManystoreBase
195 } 204 }
196 $result = $this->model->saveAll($newArr); 205 $result = $this->model->saveAll($newArr);
197 Db::commit(); 206 Db::commit();
198 - } catch (Exception $e) { 207 + } catch (\Exception $e) {
199 Db::rollback(); 208 Db::rollback();
200 $this->error('导入失败,稍后请重试'); 209 $this->error('导入失败,稍后请重试');
201 } 210 }