简体   繁体   English

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

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

I already did this but in that time I use laravel blade and laravel collective to save the form.我已经这样做了,但在那段时间我使用 laravelblade 和 laravel 集体来保存表单。 Now that I am using vue, It is a bit different for me and too tricky for me to do since im new with vuejs.现在我正在使用 vue,这对我来说有点不同,而且对我来说太棘手了,因为我是 vuejs 新手。 How can I achieve this?我怎样才能做到这一点? Can I use laravel collective in a vue component so that my job will become easy?我可以在 vue 组件中使用 laravel 集体,以便我的工作变得轻松吗? Thanks for the answers.感谢您的回答。 Here are my codes.这是我的代码。

Vue component组件

<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>

My route我的路线

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

The Controller控制器

 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');  
            }
        }
    }

method方法

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

I only have two columns in my excel to import in the database.我的 excel 中只有两列要导入数据库。 Im really new to client side so its difficult for me.我对客户端真的很陌生,所以对我来说很难。 Thanks for helping..谢谢你的帮助。。

onchange you've to upload the excel sheet in formData 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