|
|
|
@ -1,24 +1,51 @@
|
|
|
|
<template>
|
|
|
|
<template>
|
|
|
|
<el-card header="导入导出excel">
|
|
|
|
<el-card header="导入导出excel">
|
|
|
|
|
|
|
|
<h1>上传excel</h1>
|
|
|
|
|
|
|
|
<div class="flex">
|
|
|
|
<el-upload
|
|
|
|
<el-upload
|
|
|
|
|
|
|
|
style="width: 200px"
|
|
|
|
action="/excel/upload"
|
|
|
|
action="/excel/upload"
|
|
|
|
:headers="headers"
|
|
|
|
:headers="headers"
|
|
|
|
method="post"
|
|
|
|
method="post"
|
|
|
|
|
|
|
|
:on-change="handleFileChange"
|
|
|
|
|
|
|
|
:on-remove="handleFileRemove"
|
|
|
|
|
|
|
|
:auto-upload="false"
|
|
|
|
|
|
|
|
:limit="1"
|
|
|
|
|
|
|
|
:file-list="fileList"
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<el-button type="primary">上传excel</el-button>
|
|
|
|
<el-button type="primary">选择文件</el-button>
|
|
|
|
</el-upload>
|
|
|
|
</el-upload>
|
|
|
|
|
|
|
|
<el-button
|
|
|
|
|
|
|
|
class="ml-5"
|
|
|
|
|
|
|
|
type="primary"
|
|
|
|
|
|
|
|
@click="uploadExcel"
|
|
|
|
|
|
|
|
>点击上传选中文件</el-button
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<h1 class="mt-20">下载excel</h1>
|
|
|
|
<el-button
|
|
|
|
<el-button
|
|
|
|
type="primary"
|
|
|
|
type="primary"
|
|
|
|
@click="downloadExcel"
|
|
|
|
@click="downloadExcel"
|
|
|
|
>下载excel</el-button
|
|
|
|
>下载excel</el-button
|
|
|
|
>
|
|
|
|
>
|
|
|
|
</el-card>
|
|
|
|
</el-card>
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
|
|
|
class="markdown-body"
|
|
|
|
|
|
|
|
v-html="htmlStr"
|
|
|
|
|
|
|
|
></div>
|
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup lang="jsx">
|
|
|
|
<script setup lang="jsx">
|
|
|
|
import { ref } from 'vue';
|
|
|
|
import { ref } from 'vue';
|
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import axios from 'axios';
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
import { marked } from 'marked';
|
|
|
|
|
|
|
|
import { getMarkdownContent } from '@/utils/tools';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const htmlStr = ref('');
|
|
|
|
|
|
|
|
getMarkdownContent('./md/importOrExportExcel.md').then((res) => {
|
|
|
|
|
|
|
|
htmlStr.value = marked(res);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
defineOptions({
|
|
|
|
name: 'DemoImportOrExportExcel',
|
|
|
|
name: 'DemoImportOrExportExcel',
|
|
|
|
@ -28,10 +55,11 @@ const headers = ref({
|
|
|
|
Authorization: Cookies.get('Authorization') || '',
|
|
|
|
Authorization: Cookies.get('Authorization') || '',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 下载excel文件(这里是假的接口,不能触发真实下载excel)
|
|
|
|
const downloadExcel = () => {
|
|
|
|
const downloadExcel = () => {
|
|
|
|
axios({
|
|
|
|
axios({
|
|
|
|
url: '/excel/download',
|
|
|
|
url: '/excel/download',
|
|
|
|
method: 'get',
|
|
|
|
method: 'post',
|
|
|
|
// 重要,设置responseType为blob
|
|
|
|
// 重要,设置responseType为blob
|
|
|
|
responseType: 'blob',
|
|
|
|
responseType: 'blob',
|
|
|
|
headers: headers.value,
|
|
|
|
headers: headers.value,
|
|
|
|
@ -39,13 +67,79 @@ const downloadExcel = () => {
|
|
|
|
const name = response.headers['content-disposition'].split(';')[1].split('=')[1];
|
|
|
|
const name = response.headers['content-disposition'].split(';')[1].split('=')[1];
|
|
|
|
// 重要,类型要设置为'application/vnd.ms-excel;charset=utf-8'
|
|
|
|
// 重要,类型要设置为'application/vnd.ms-excel;charset=utf-8'
|
|
|
|
let blob = new Blob([response.data], { type: 'application/vnd.ms-excel;charset=utf-8' });
|
|
|
|
let blob = new Blob([response.data], { type: 'application/vnd.ms-excel;charset=utf-8' });
|
|
|
|
let link = document.createElement('a');
|
|
|
|
downloadBlobFile(blob, name);
|
|
|
|
link.style.display = 'none';
|
|
|
|
// 这里实现一个功能,将blob文件直接通过console.log打印出来(可以直接在控制台查看blob文件的数据)
|
|
|
|
link.href = URL.createObjectURL(blob);
|
|
|
|
showBlobFile(blob);
|
|
|
|
link.setAttribute('download', name);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提取一个公共下载blob文件的方法
|
|
|
|
|
|
|
|
const downloadBlobFile = (blob, fileName) => {
|
|
|
|
|
|
|
|
// 创建下载的链接
|
|
|
|
|
|
|
|
const downloanUrl = window.URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
// 创建a标签, 准备好a标签的对应属性
|
|
|
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
|
|
|
a.style.display = 'none';
|
|
|
|
|
|
|
|
a.href = downloanUrl;
|
|
|
|
|
|
|
|
a.download = fileName;
|
|
|
|
|
|
|
|
// 将a标签插入到body中,然后点击触发下载,最后移除a标签
|
|
|
|
document.body.appendChild(link);
|
|
|
|
document.body.appendChild(link);
|
|
|
|
link.click();
|
|
|
|
link.click();
|
|
|
|
document.body.removeChild(link);
|
|
|
|
document.body.removeChild(link);
|
|
|
|
|
|
|
|
// 释放url
|
|
|
|
|
|
|
|
window.URL.revokeObjectURL(downloanUrl);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 提取一个公共打印blob文件的方法
|
|
|
|
|
|
|
|
* 发散思维,如果是正常的文件,那么这个文件可能很大
|
|
|
|
|
|
|
|
* 如果报错信息直接通过blob文件输出,那我们应该怎么知道这个是报错的blob文件信息还是一个正常的文件信息
|
|
|
|
|
|
|
|
* 我们可以通过blob文件的大小size来判断
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
const showBlobFile = (blob) => {
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
|
|
|
console.log(e.target.result);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsText(blob);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fileList = ref([]);
|
|
|
|
|
|
|
|
const handleFileChange = (file, fileList) => {
|
|
|
|
|
|
|
|
console.log(file, fileList, '上传文件列表变化函数');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFileRemove = (file, fileList) => {
|
|
|
|
|
|
|
|
console.log(file, fileList, '删除文件列表变化函数');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// 上传excel(这里是假的接口,不能触发真实上传excel)
|
|
|
|
|
|
|
|
const uploadExcel = () => {
|
|
|
|
|
|
|
|
console.log(fileList);
|
|
|
|
|
|
|
|
// 重点关注这里,一般来说上传文件时基本都是FormData格式,而不是json对象
|
|
|
|
|
|
|
|
// 创建一个FormData
|
|
|
|
|
|
|
|
let formData = new FormData();
|
|
|
|
|
|
|
|
// 添加属性为file,value为选中的二进制文件的blob
|
|
|
|
|
|
|
|
formData.append('file', fileList.value[0].raw);
|
|
|
|
|
|
|
|
// 添加属性为parmas1,value为(我是其他参数1)字符串
|
|
|
|
|
|
|
|
formData.append('parms1', '我是其他参数1');
|
|
|
|
|
|
|
|
// 添加属性为parmas2,value为(我是其他参数2)字符串
|
|
|
|
|
|
|
|
formData.append('parms2', '我是其他参数2');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 下面演示错误写法,这里重点注意,下面注释的这种写法是错误的,所有的参数对象都应该放在FormData里面
|
|
|
|
|
|
|
|
// let formData = new FormData();
|
|
|
|
|
|
|
|
// formData.append('file', fileList.value[0].raw);
|
|
|
|
|
|
|
|
// let parmam = {
|
|
|
|
|
|
|
|
// file: formData,
|
|
|
|
|
|
|
|
// parmam1: '我是其他参数1',
|
|
|
|
|
|
|
|
// parmam2: '我是其他参数2',
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
axios({
|
|
|
|
|
|
|
|
url: '/excel/upload',
|
|
|
|
|
|
|
|
method: 'post',
|
|
|
|
|
|
|
|
data: formData,
|
|
|
|
|
|
|
|
headers: headers.value,
|
|
|
|
|
|
|
|
}).then((response) => {
|
|
|
|
|
|
|
|
console.log(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
</script>
|
|
|
|
|