zoukankan      html  css  js  c++  java
  • tp6之批量导入1

     1 <?php
     2 
     3 
     4 namespace appapicontrollerconsole;
     5 
     6 use appapimodelMonitoringViolationType;
     7 use PhpOfficePhpSpreadsheetIOFactory;
     8 use thinkRequest;
     9 
    10 class Violation
    11 {
    12 
    13     /**
    14      * 批量导入
    15      * @param Request $request
    16      * @param MonitoringViolationType $MonitoringViolationType
    17      * @return 	hinkResponse
    18      */
    19     public function importPost(Request $request, MonitoringViolationType $MonitoringViolationType)
    20     {
    21         $file = $request->file();
    22         $type = $request->post('type/d', 1);
    23         validate([
    24             'file' => 'require|file|fileExt:xlsx',
    25         ])->check($file);
    26 
    27         $path = app('filesystem')->disk('local')->putFile('import', $file['file'], function(){
    28             return 'violation_'.time();
    29         });
    30 
    31         $objRead = IOFactory::createReader('Xlsx');
    32         $objRead->setReadDataOnly(true);    //则只读内容,提升读取Excel效率
    33         $obj = $objRead->load(config('filesystem.disks.local.root').'/'.$path); //建立excel对象
    34         $currSheet = $obj->getSheet(0); //获取指定的sheet表
    35         $rowCount = $currSheet->getHighestRow(); //获取总行数
    36 
    37         $data = [];
    38         //读取内容
    39         for ($row = 9; $row <= $rowCount; $row++) {
    40             //分类
    41             $data['A'] = $currSheet->getCell('A'.$row)->getValue();
    42             $data['B'] = $currSheet->getCell('B'.$row)->getValue();
    43             $data['C'] = $currSheet->getCell('C'.$row)->getValue();
    44             $data['D'] = $currSheet->getCell('D'.$row)->getValue();
    45             $data['E'] = $currSheet->getCell('E'.$row)->getValue();
    46             $data['F'] = $currSheet->getCell('F'.$row)->getValue();
    47             $data['G'] = $currSheet->getCell('G'.$row)->getValue();
    48 
    49             $pid = 0;
    50             foreach($data as $key => $val) {
    51                 if (!empty($val)) {
    52                     $where = $pid == 0 ? ['name' => $val, 'type' => $type] : ['name' => $val, 'pid' => $pid, 'type' => $type];
    53                     $id = $MonitoringViolationType->where($where)->value('id', 0);
    54                     if (empty($id)) {
    55                         $pid = $MonitoringViolationType->insertGetId(['name' => $val, 'pid' => $pid, 'is_directly' => 0, 'type' => $type]);
    56                     } else {
    57                         $pid = $id;
    58                     }
    59                 }
    60             }
    61 
    62             //内容
    63             $name = $currSheet->getCell('H'.$row)->getValue() ?? '';
    64             $remark = $currSheet->getCell('I'.$row)->getValue() ?? '';
    65 
    66             if (!empty($name)) {
    67                 if (!$MonitoringViolationType->where(['name' => $name, 'pid' => $pid, 'type' => $type])->value('id')) {
    68                     $MonitoringViolationType->insert(['name' => $name, 'pid' => $pid, 'remark' => $remark, 'is_directly' => 0, 'type' => $type]);
    69                 }
    70             }
    71         }
    72 
    73         app('filesystem')->disk('local')->delete($path);
    74 
    75         return JsonSuccess('导入成功');
    76     }
    77 
    78     /**
    79      * 下载模板表格
    80      * @return
    81      */
    82     public function tempGet()
    83     {
    84         $file = getcwd().'/storage/违法类型模块表格.xlsx';
    85         header('content-type:application/octet-stream');
    86         header('content-disposition:attachment; filename=' . basename($file));
    87         header('content-length:' . filesize($file));
    88         return file_get_contents($file);
    89        //return download(config('filesystem.disks.public.root').'/'.'违法类型模块表格.xlsx');
    90     }
    91 }
    执指之手,与之偕老!
  • 相关阅读:
    utf8和utf8mb4的区别
    【JSP 标签】选择判断c:choose
    【JSP 标签】格式化日期
    【Spring 核心】AOP 面向切面编程
    【Spring 核心】高级装配
    【Spring 核心】装配bean(三)XML配置
    【Spring 核心】装配bean(二) JavaConfig装配
    【Spring 核心】装配Bean(一) 自动化装配
    【Js应用实例】图片预览
    【Js应用实例】限制上传图片大小
  • 原文地址:https://www.cnblogs.com/silen0119/p/15128274.html
Copyright © 2011-2022 走看看