vue如何實現(xiàn)在線編輯excel
vue實現(xiàn)在線編輯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,
},
},
},
/*數(shù)組*/
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導(dǎo)出excel模板
1、首先需要導(dǎo)入第三方插件xlsx(最好是指定版本,要不然容易報錯)
npm install --save xlsx@0.17.0
后續(xù)可能還會用到導(dǎo)入導(dǎo)出,也需要安裝第三方插件
npm install --save file-saver@2.0.5
2、哪個頁面需要下載excel模板就到哪里導(dǎo)入
import XLSX from "xlsx"
3、給下載模板按鈕綁定事件
<el-button @click="downloadExcel">嗨嗨嗨</el-button>
4、下載模板執(zhí)行內(nèi)容
downloadExcel() {
let excelData = [
[ '姓名', '電話', '生日', 'xx', "xx", "……"]//這里是表頭數(shù)據(jù)
]
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')
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3 elementPlus 表格實現(xiàn)行列拖拽及列檢索功能(完整代碼)
本文通過實例代碼給大家介紹vue3 elementPlus 表格實現(xiàn)行列拖拽及列檢索功能,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10
Vue 實現(xiàn)html中根據(jù)類型顯示內(nèi)容
今天小編大家分享一篇Vue 實現(xiàn)html中根據(jù)類型顯示內(nèi)容,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
vue中json格式化顯示數(shù)據(jù)(vue-json-viewer)
這篇文章主要給大家介紹了關(guān)于vue中json格式化顯示數(shù)據(jù)(vue-json-viewer)的相關(guān)資料,Vue-json-viewer是一個Vue組件,用于在Vue應(yīng)用中顯示JSON數(shù)據(jù)的可視化工具,需要的朋友可以參考下2024-05-05
vue3.x?的shallowReactive?與?shallowRef?使用場景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧2025-02-02

