Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

메모장

Vue Excel JSON 형식으로 Parsing 본문

JavaScript

Vue Excel JSON 형식으로 Parsing

doopang 2023. 1. 17. 11:07

onload sample.xlsx
8.4 kB

Terminal

npm install xlsx

 

Main.vue

<template>
  <input type="file" @change="onloadFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
</template>

<script>
import * as XLSX from "xlsx";
import excel from "@/api/excel";

export default {
  data() {
return {
  items: []
}
  },
  methods: {
onloadFile(event) {
      const file = event.target.files[0];
      let reader = new FileReader();
      reader.onload = (e) => {
        let data = reader.result;
        // let data = e.target.result;
        let workbook = XLSX.read(data, {type: 'binary'});
        workbook.SheetNames.forEach(sheetName => {
          const roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
          this.items.push(roa);
          excel.onloadFile(JSON.parse(JSON.stringify(this.items))[0]);
        });
      };
      reader.readAsBinaryString(file);
    },
  }
}
</script>

 

excel.js

import axios from '@/api/axios'

const onloadFile = async (params, result) => {
  console.log('params', params)
  const options = {
    method: 'POST',
    url: '/excel/onloadFile',
    data: params
  }
  await axios(options)
    .then((response) => {
      if (response.data.code === 0) {
        result(response.data.success)
      } else {
        result(null)
      }
    })
}

export default {
  onloadFile
}

 

Spring Boot
Controller

@PostMapping("/onloadFile")
public CommonResult onloadFile(@RequestBody List<Map<String, Object>> paramList) {
  for (Map<String, Object> param : paramList) {
    System.out.println("이름: " + param.get("이름"));
  }
  return responseService.getSuccessResult();
}

'JavaScript' 카테고리의 다른 글

Vue vee-validate  (0) 2023.03.14
FormData 로그 보기  (0) 2023.02.20
Storage  (0) 2023.01.27
Vue 우편주소 검색  (0) 2022.12.29
Vue npm 실행 에러 날 때  (0) 2022.04.10