繁体   English   中英

如何在 vue 组件中使用 Maatwebsite 导入 excel 文件?

[英]How do I import a excel file using Maatwebsite in a vue component?

我已经这样做了,但在那段时间我使用 laravelblade 和 laravel 集体来保存表单。 现在我正在使用 vue,这对我来说有点不同,而且对我来说太棘手了,因为我是 vuejs 新手。 我怎样才能做到这一点? 我可以在 vue 组件中使用 laravel 集体,以便我的工作变得轻松吗? 感谢您的回答。 这是我的代码。

组件

<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title" id="exampleModalLabel">Import CSV</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
                  <form @submit.prevent="importRoom()">
                <div class="modal-body">
                    <div class="form-group">
                      <label>Select CSV</label>
                      <input
                        v-on:change ="i dont have a method yet"
                        type="file"
                        name="template"
                        id="template"
                        class="form-control"
                        :class="{ 'is-invalid': form.errors.has('template') }"
                      />
                      <has-error :form="form" field="bldg"></has-error>
                    </div>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Save changes</button>
                </div>
                </form>
              </div>
            </div>
          </div>

我的路线

Route::post('/importRoom','RoomController@import');

控制器

 public function import(Request $request){
        if($request->hasFile('template')){
            $path = $request->file('template')->getRealPath();
            $data = \Excel::load($path)->get();

            if($data->count() > 0){
                $rows = $data->toArray();
                foreach ($rows as $row) {
                    $inserts[]=[
                        'room_desc' => $row['room_desc'],
                        'bldg' => $row['bldg'],
                    ];
                }
            }
            $chuncked = array_chunk($inserts, 10);
            if(empty($inserts)){
                dd('Request data does not have any files to import.');  
            }
            else {
                foreach($chuncked as $inserts){
                    \DB::table('rooms')->insert($inserts);
                 }
                dd('record inserted');  
            }
        }
    }

方法

  importRoom(){
            axios.post('/importRoom')
              .then(()=>{
                console.log('imported')
              })
          }

我的 excel 中只有两列要导入数据库。 我对客户端真的很陌生,所以对我来说很难。 谢谢你的帮助。。

onchange 你必须上传 formData 中的 Excel 表格

getExcelData(e) {
    const formData = new FormData();
    const file = e.target.files[0];
    formData.append('file', file);
    axios.post('url', formData)
            .then(response => {
                //Code to play with api response with excel data
            })
            .catch(error => {
                //Catch errors
            });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM