Dcat Admin实现简单的excel导入功能

实现效果

 

Dcat-admin实现简单的excel导入功能
 
Dcat-admin实现简单的excel导入功能

 

1. 安装 maatwebsite/excel

composer require maatwebsite/excel
2. 在控制器中添加按钮
 
use App\Admin\Actions\Modal\memberModal;
$grid->tools(function  (Grid\Tools  $tools)  { 
    //Excel导入
    $tools->append(new  memberModal());  
});

3. 创建文件

 

Dcat-admin实现简单的excel导入功能
 

app/admin/actions/Modal/memberModal.php

<?php
<p>namespace App\Admin\Actions\Modal;</p>
<p>use App\Admin\Actions\Form\memberForm;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\Tools\AbstractTool;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;</p>
<p>class memberModal extends AbstractTool
{
/**
* @return string
*/
protected $title = 'Title';</p>
<pre><code>public function render()
{
    $id = &quot;reset-pwd-{$this-&gt;getKey()}&quot;;

    // 模态窗
    $this-&gt;modal($id);

    return &lt;&lt;&lt;HTML
</code></pre>
<span class="grid-expand" data-toggle="modal" data-target="#{$id}">
   <a href="javascript:void(0)"><button class="btn btn-outline-info ">上传Excel并导入数据</button></a>
</span>
HTML;
    }
<pre><code>protected function modal($id)
{
    $form = new memberForm();

    Admin::script('Dcat.onPjaxComplete(function () {
        $(&quot;.modal-backdrop&quot;).remove();
        $(&quot;body&quot;).removeClass(&quot;modal-open&quot;);
    }, true)');

    // 通过 Admin::html 方法设置模态窗HTML
    Admin::html(
        &lt;&lt;&lt;HTML
</code></pre>
<div class="modal fade" id="{$id}">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">导入数据</h4>
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        {$form->render()}
      </div>
    </div>
  </div>
</div>
HTML
        );
    }
<pre><code>/**
 * @param Model|Authenticatable|HasPermissions|null $user
 *
 * @return bool
 */
protected function authorize($user): bool
{
    return true;
}

/**
 * @return array
 */
protected function parameters()
{
    return [];
}
</code></pre>
<p>}

app/admin/actions/Form/memberForm.php

response()->success('数据导入成功')->refresh();
        } catch (\Exception $e) {
            return $this->response()->error($e->getMessage());
        }
    }
public function form()
{
    $this-&gt;file('file', '上传数据(Excel)')-&gt;rules('required', ['required' =&gt; '文件不能为空'])-&gt;move('admin/upload/');

}

}

app/admin/actions/Imports/memberImport.php

first();
        if ($user) {
            return null;
        }
        return new Member([
            'username' => $row[0],
            'name' => $row[1],
        ]);
}


/**
 * 从第几行开始处理数据 就是不处理标题
 * @return int
 */
public function startRow(): int
{
    return 2;
}

}

4.Model 允许写入

class Member extends Model
{
  protected $fillable = ['name','username'];
}

5. 导入 Excel 样式

Dcat-admin实现简单的excel导入功能
 
原文作者:mink7
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。
 

本站部分文章、图片等数据来自互联网,一切版权均归源网站或源作者所有

如果侵犯了你的权益请来信告知删除。邮箱:1029167904@qq.com


发表评论

评论列表,共 0 条评论

cover cover cover

  • cover

    搁浅 周杰伦

  • cover

    呼啸的风 侯明昊

  • cover

    如果可以 韦礼安