如何在Vue组件中使用Maatwebsite导入Excel文件?

时间:2020-01-29 14:09:06

标签: javascript php laravel vue.js

我已经这样做了,但是那时候我使用laravel刀片和laravel集体来保存表格。现在,我正在使用vue,这对我来说有点不同,而且因为vuejs是我的新手,所以我做起来也很棘手。我该如何实现?我可以在Vue组件中使用laravel Collective来简化我的工作吗?感谢您的回答。这是我的代码。

Vue组件

<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中只有两列要导入数据库。我对客户端来说真的很新,所以对我来说很难。感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

更改后,您必须在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
            });
}