반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- bucket max-key
- CSS
- kotlin
- bucket cors
- Vue
- MySQL
- onsenui
- 동일 프로그램
- 프로세스 방지
- 동일 프로세스
- Android
- nodejs
- JavaScript
- sequelize
- Electron
- c#
- electron-nuxt
- v-text-field height
- sort
- onsen-ui
- v-select
- NUXT
- vuejs
- naver storage
- xlsx
- Vuetify
- naver storage bucket error
- f035d
- vuetifyjs
- error
Archives
- Today
- Total
앙큼한 개발기록
[vue/nuxt] xlsx file export 본문
사용한 템플릿 : nuxt
library
- xlsx
- file-saver
예제 소스 코드:
<template>
<div>
<button @click="onClickPrint">출력</button>
<table :id="tableId">
<thead>
<tr>
<th v-for="[key, value] in Object.entries(header)">{{ value }}</th>
</tr>
</thead>
<tbody>
<tr v-for="product in list">
<td>{{ product.index }}</td>
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
const FILE_NAME = "test"
const SHEET_NAME = "테스트 시트"
const FILE_EXTENSION = ".xlsx"
import { saveAs } from 'file-saver'
const XLSX = require('xlsx')
export default {
name: "ExportTable",
data() {
return {
header: {
name: "이름",
index: "번호",
price: "가격"
},
list: [],
tableId: "tableData"
}
},
created() {
console.log("page created")
let i = 0
do {
this.list.push({
name: `test ${i}`,
index: i,
price: Math.floor(Math.random() * 1000)
})
i++
}
while (i < 100)
},
mounted() {
console.log("page mounted")
},
methods: {
onClickPrint() {
this.exportExcel()
},
exportExcel() {
// step 1. workbook 생성
let wb = XLSX.utils.book_new();
// step 2. worksheet 생성
let ws = this.getWorksheet()
// step 3. workbook 에 새로만든 워크시트에 이름을 주고 붙인다.
XLSX.utils.book_append_sheet(wb, ws, SHEET_NAME)
// step 4. 엑셀 파일 만들기
let wbt = XLSX.write(wb, {
bookType: 'xlsx',
type: 'binary',
})
// step 5. 엑셀 파일 내보내기
saveAs(new Blob([this.s2ab(wbt)], {type: "application/octet-stream"}), this.getExcelFileName())
},
getExcelData: function () {
return document.getElementById(this.tableId)
},
getWorksheet: function () {
return XLSX.utils.table_to_sheet(this.getExcelData(), {
raw: true,
cellStyles: true
})
},
getExcelFileName() {
let year = new Date().getFullYear()
let month = new Date().getMonth()
let date = new Date().getDate()
return FILE_NAME + '_' + `${year}-${month}-${date}` + FILE_EXTENSION
},
s2ab(s) {
//convert s to arrayBuffer
let buf = new ArrayBuffer(s.length)
//create uint8array as viewer
let view = new Uint8Array(buf)
//convert to octet
for (let i = 0; i < s.length; i++) {
view[i] = s.charCodeAt(i) & 0xFF
}
return buf
},
}
}
</script>
<style scoped>
</style>
'개발 > vue & nuxt' 카테고리의 다른 글
[naver storage] bucket cors 오류 (0) | 2023.08.16 |
---|---|
[vuetifyjs] v-select 높이, 화살표 변경 (0) | 2023.06.22 |
[vuetifyjs] v-text-field 높이값 조절 (0) | 2023.06.22 |
[vue div resize] 내부 화면 크기 조절하기 (onResize library를 겸한) (0) | 2023.04.02 |