• TP6框架--EasyAdmin学习笔记:excel的数据,图片处理


    这是我写的学习EasyAdmin的第七章,这一章我给大家分享下如何处理excel中的数据,图片

    原理就是使用easyadmin中封装好的phpexcel,来进行数据的导入,view层我们需要一个用了导入excel的页面,代码如下:

    <div class="layuimini-container">
        <form id="app-form" class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label">导入excel:</label>
                <div class="layui-input-block">
                   <button type="button" class="layui-btn" id="test1">
                      <i class="layui-icon">�</i>上传文件
                    </button>
                </div>
            </div>
        </form>
    </div>
    <script src="/static/plugs/layui-v2.5.6/layui.all.js?v=2.0.0" charset="utf-8"></script>
    <script src="/static/plugs/jquery-3.4.1/jquery-3.4.1.min.js?v=2.0.0" charset="utf-8"></script>
    <script>
    layui.use('upload', function(){
      var upload = layui.upload;
      var loading;
      //执行实例
      var uploadInst = upload.render({
        elem: '#test1' //绑定元素
        ,url: '/admin/mall.goodsone/import' //上传接口
        ,accept: 'file'
        ,choose:function(){
            loading = layer.load(0, {
                shade: false,
                time: 60*60*1000
            });
        }
        ,done: function(res){
          //上传完毕回调
          layer.close(loading);
          if(res.msg == '导入成功'){
            layer.msg(res.msg); 
          }
        }
        ,error: function(){
            layer.close(loading);
          //请求异常回调
    
        }
      });
    });
    
    </script>

    js中加入以下代码:

     接下来就是重点,controller部分的代码,这里先在前面导入easyadmin封装好的phpexcel

    use EasyAdmin	oolCommonTool;
    use jianyanexcelExcel;

    在方法里写入下面代码

                $file = request()->file('file');
                ini_set('memory_limit','1024M');
                $data =  Excel::import($file);        

    $data里就是处理好的数据数组,但是实际用的时候会发现,图片没法识别,这里给出解决方法,亲测有用,大家可以照着思路来就行

    PHPSpreadsheet

    首先安装phpspreadsheet,由于线上服务器PHP版本是PHP5.6,所以需要安装兼容PHP5.6的版本,这里安装1.8.2版本

    composer require phpoffice/phpspreadsheet=1.8.2

    然后就可以在项目里使用了

    use PhpOfficePhpSpreadsheetCellCoordinate;
    use PhpOfficePhpSpreadsheetIOFactory;
    
    $imageFilePath = './uploads/imgs/'; //图片本地存储的路径
    if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建
        mkdir($imageFilePath, 0777, true);
    }
    
    try {
        $inputFileName = './files/1.xlsx';  //包含图片的Excel文件
        $objRead = IOFactory::createReader('Xlsx');
        $objSpreadsheet = $objRead->load($inputFileName);
        $objWorksheet = $objSpreadsheet->getSheet(0);
        $data = $objWorksheet->toArray();
    
        foreach ($objWorksheet->getDrawingCollection() as $drawing) {
            list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
            $imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
    
            switch ($drawing->getExtension()) {
                case 'jpg':
                case 'jpeg':
                    $imageFileName .= '.jpg';
                    $source = imagecreatefromjpeg($drawing->getPath());
                    imagejpeg($source, $imageFilePath . $imageFileName);
                    break;
                case 'gif':
                    $imageFileName .= '.gif';
                    $source = imagecreatefromgif($drawing->getPath());
                    imagegif($source, $imageFilePath . $imageFileName);
                    break;
                case 'png':
                    $imageFileName .= '.png';
                    $source = imagecreatefrompng($drawing->getPath());
                    imagepng($source, $imageFilePath, $imageFileName);
                    break;
            }
            $startColumn = ABC2decimal($startColumn);
            $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
        }
        dump($data);die();
    } catch (Exception $e) {
        throw $e;
    }
    
    public function ABC2decimal($abc)
    {
        $ten = 0;
        $len = strlen($abc);
        for($i=1;$i<=$len;$i++){
            $char = substr($abc,0-$i,1);//反向获取单个字符
    
            $int = ord($char);
            $ten += ($int-65)*pow(26,$i-1);
        }
        return $ten;
    }

    可以看到,图片被读取并存到了本地服务器中

    PHPExcel

    PHPExcel实现从Excel文件里读取内容的方法和phpspreadsheet几乎一样,毕竟phpspreadsheet就是在PHPExcel基础上写的,不过PHPExcel由于已经被废弃了,所以建议优先使用phpspreadsheet,如果原来项目里一直使用了PHPExcel也可以继续使用PHPExcel的方法

    use PHPExcel_IOFactory;
    use PHPExcel_Cell;
    
    try {
        $inputFileName = './files/1.xlsx';
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch (Exception $e) {
        die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }
    
    $sheet = $objPHPExcel->getSheet(0);
    $data = $sheet->toArray(); //该方法读取不到图片,图片需单独处理
    $imageFilePath = './uploads/imgs/'; //图片本地存储的路径
    if (!file_exists($imageFilePath)) {
        mkdir($imageFilePath, 0777, true);
    }
    
    //处理图片
    foreach ($sheet->getDrawingCollection() as $img) {
        list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates()); //获取图片所在行和列
        $imageFileName = $img->getCoordinates() . mt_rand(1000, 9999);
        switch($img->getExtension()) {
            case 'jpg':
            case 'jpeg':
                $imageFileName .= '.jpeg';
                $source = imagecreatefromjpeg($img->getPath());
                imagejpeg($source, $imageFilePath.$imageFileName);
                break;
            case 'gif':
                $imageFileName .= '.gif';
                $source = imagecreatefromgif($img->getPath());
                imagejpeg($source, $imageFilePath.$imageFileName);
                break;
            case 'png':
                $imageFileName .= '.png';
                $source = imagecreatefrompng($img->getPath());
                imagejpeg($source, $imageFilePath.$imageFileName);
                break;
        }
        $startColumn = ABC2decimal($startColumn);
        $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
    
    }
    var_dump($data);
    
    public function ABC2decimal($abc)
    {
        $ten = 0;
        $len = strlen($abc);
        for($i=1;$i<=$len;$i++){
            $char = substr($abc,0-$i,1);//反向获取单个字符
    
            $int = ord($char);
            $ten += ($int-65)*pow(26,$i-1);
        }
        return $ten;
    }

    这部分思路我参考于https://www.cnblogs.com/itbsl/p/11883458.html

    如果本文对你有所帮助,麻烦你点个赞。

  • 相关阅读:
    Mondriaan's Dream POJ
    H
    Superdoku Kattis
    Corn Fields POJ
    旅行的意义 Gym
    G
    J
    Welcome Party ZOJ
    redis入门到精通系列(三):key的通用操作和redis内部db的通用操作
    redis入门到精通系列(二):redis操作的两个实践案例
  • 原文地址:https://www.cnblogs.com/smileZAZ/p/15005135.html
Copyright © 2020-2023  润新知