vue如何實現在線編輯excel
更新時間:2023年10月24日 08:45:29 作者:不會么么噠
這篇文章主要介紹了vue如何實現在線編輯excel問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vue實現在線編輯excel
不支持ie 支持edge 需要ie請換方法
vue不再贅述
安裝x-data-spreadsheet
npm install x-data-spreadsheet
<template> <div id="x-spreadsheet-demo"></div> </template> <script> import Spreadsheet from 'x-data-spreadsheet'; import zhCN from 'x-data-spreadsheet/dist/locale/zh-cn'; export default { data() { return { options:{ mode: 'edit', showToolbar: true, showGrid: true, showContextmenu: true, view: { /*寬高,因為我是自定義的所以-----*/ /*height: () => document.documentElement.clientHeight - 300, width: () => document.documentElement.clientWidth - 300,*/ height: () => document.documentElement.clientHeight - 64, width: () => document.documentElement.clientWidth - 150, }, formats: [], fonts: [], formula: [], row: { len: 100, height: 25, }, col: { len: 26, width: 100, indexWidth: 60, minWidth: 60, }, /*freeze: 'C3',*/ style: { bgcolor: '#ffffff', align: 'left', valign: 'middle', textwrap: false, textDecoration: 'normal', strikethrough: false, color: '#0a0a0a', font: { name: 'Helvetica', size: 10, bold: false, italic: false, }, }, }, /*數組*/ optionss:[{ name:'aaaa', /*freeze: 'C3',*/ styles: [ { /*小框背景色*/ bgcolor: '#f4f5f8', textwrap: true, color: '#900b09', border: { top: ['thin', '#0366d6'], bottom: ['thin', '#0366d6'], right: ['thin', '#0366d6'], left: ['thin', '#0366d6'], }, },{ /*小框背景色*/ bgcolor: '#000000', textwrap: true, color: '#000000', border: { top: ['thin', '#000000'], bottom: ['thin', '#000000'], right: ['thin', '#000000'], left: ['thin', '#000000'], }, }, ], /*合并單元格*/ merges: [ 'C3:E4', ], rows: { /*第1行*/ 1: { cells: { /*第0列 2列*/ 0: { text: 'testingtesttestetst' }, 2: { text: 'testing' }, }, }, 2: { cells: { 0: { text: 'render', style: 0 }, 1: { text: 'Hello' }, 2: { text: 'haha', merge: [1, 2] }, } }, 8: { cells: { 8: { text: 'border test', style: 0 }, } } }, },{ name:'aaaa', /*freeze: 'C3',*/ styles: [ { /*小框背景色*/ bgcolor: '#f4f5f8', textwrap: true, color: '#900b09', border: { top: ['thin', '#0366d6'], bottom: ['thin', '#0366d6'], right: ['thin', '#0366d6'], left: ['thin', '#0366d6'], }, },{ /*小框背景色*/ bgcolor: '#000000', textwrap: true, color: '#000000', border: { top: ['thin', '#000000'], bottom: ['thin', '#000000'], right: ['thin', '#000000'], left: ['thin', '#000000'], }, }, ], /*合并單元格*/ merges: [ 'C3:E4', ], rows: { /*第1行*/ 1: { cells: { /*第0列 2列*/ 0: { text: 'testingtesttestetst' }, 2: { text: 'testing' }, }, }, 2: { cells: { 0: { text: 'render', style: 0 }, 1: { text: 'Hello' }, 2: { text: 'haha', merge: [1, 2] }, } }, 8: { cells: { 8: { text: 'border test', style: 0 }, } } }, }] } }, methods: { }, mounted:function(){ /*中文*/ Spreadsheet.locale('zh-cn', zhCN); new Spreadsheet('#x-spreadsheet-demo', this.options).loadData(this.optionss).change((data) => { console.log(data) }); } } </script> <style> </style>
vue導出excel模板
1、首先需要導入第三方插件xlsx(最好是指定版本,要不然容易報錯)
npm install --save xlsx@0.17.0
后續(xù)可能還會用到導入導出,也需要安裝第三方插件
npm install --save file-saver@2.0.5
2、哪個頁面需要下載excel模板就到哪里導入
import XLSX from "xlsx"
3、給下載模板按鈕綁定事件
<el-button @click="downloadExcel">嗨嗨嗨</el-button>
4、下載模板執(zhí)行內容
downloadExcel() { let excelData = [ [ '姓名', '電話', '生日', 'xx', "xx", "……"]//這里是表頭數據 ] let ws = XLSX.utils.aoa_to_sheet(excelData) let wb = XLSX.utils.book_new() XLSX.utils.book_append_sheet(wb, ws, '工作簿名稱') XLSX.writeFile(wb, '保存的文件名.xlsx') }
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue3 elementPlus 表格實現行列拖拽及列檢索功能(完整代碼)
本文通過實例代碼給大家介紹vue3 elementPlus 表格實現行列拖拽及列檢索功能,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10vue中json格式化顯示數據(vue-json-viewer)
這篇文章主要給大家介紹了關于vue中json格式化顯示數據(vue-json-viewer)的相關資料,Vue-json-viewer是一個Vue組件,用于在Vue應用中顯示JSON數據的可視化工具,需要的朋友可以參考下2024-05-05vue3.x?的shallowReactive?與?shallowRef?使用場景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應式數據的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧2025-02-02