导入导出
约 88 字小于 1 分钟
2025-02-06
<!-- 导入需要的包 (一定要放到index.html中的head标签里)-->
<script src="https://cdn.staticfile.net/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<script src="https://cdn.staticfile.net/xlsx/0.18.2/xlsx.full.min.js"></script>表格导出
vue2
vue3
`excelBtn`设置为`true`即可开启导出功能
<template>
<tvue-crud :option="option"
:data="data"></tvue-crud>
</template>
<script setup>
import { ref } from 'vue';
const data = ref([
{
text1: '内容1-1',
text2: '内容1-2',
deep: {
text3: '内容1-3',
},
},
{
text1: '内容2-1',
text2: '内容2-2',
deep: {
text3: '内容2-3',
},
},
]);
const option = ref({
excelBtn: true,
column: [
{
label: '列内容1',
prop: 'text1',
},
{
label: '列内容2',
prop: 'text2',
},
{
label: '列内容3',
prop: 'text3',
bind: 'deep.text3',
},
],
});
</script>表格导入
vue2
vue3
<template>
<div style="display:flex;">
<el-button type="primary"
@click="handleGet">下载模版</el-button>
<el-upload :auto-upload="false"
:show-file-list="false"
action="action"
:on-change="handleChange">
<el-button type="primary">导入 excel</el-button>
</el-upload>
</div>
<br />
<tvue-crud :option="option"
:data="data"></tvue-crud>
</template>
<script setup>
import { ref, getCurrentInstance } from 'vue';
let { proxy } = getCurrentInstance()
const data = ref([]);
const option = ref({
column: [
{ label: 'id', prop: 'id' },
{ label: '姓名', prop: 'name' },
{ label: '年龄', prop: 'sex' },
],
});
function handleGet () {
window.open('/cdn/demo.xlsx');
}
function handleChange (file, fileList) {
proxy.$Export.xlsx(file.raw).then((data) => {
data.value = data.results;
});
}
</script>