chore: 导出excel demo

This commit is contained in:
dap 2024-09-13 09:56:05 +08:00
parent 095eab6db4
commit a623f81fec
4 changed files with 57 additions and 5 deletions

View File

@ -49,7 +49,8 @@
"download": "Download", "download": "Download",
"sync": "Sync", "sync": "Sync",
"refresh": "Refresh", "refresh": "Refresh",
"generate": "Generate" "generate": "Generate",
"downloadLoading": "Downloading... Please wait."
} }
} }
} }

View File

@ -49,7 +49,8 @@
"download": "下载", "download": "下载",
"sync": "同步", "sync": "同步",
"refresh": "刷新", "refresh": "刷新",
"generate": "生成" "generate": "生成",
"downloadLoading": "下载中, 请稍后..."
} }
} }
} }

View File

@ -1,6 +1,35 @@
import { openWindow } from '..'; import { $t } from '@vben/locales';
import { message } from 'ant-design-vue';
import { dataURLtoBlob, urlToBase64 } from './base64Conver'; import { dataURLtoBlob, urlToBase64 } from './base64Conver';
/**
* excel文件
* @param [func] axios函数
* @param [fileName] xlsx后缀
* @param [requestData]
* @param [withRandomName]
*
* @return void
*/
export async function downloadExcel(
func: (data?: any) => Promise<Blob>,
fileName: string,
requestData: any = {},
withRandomName = true,
) {
const hideLoading = message.loading($t('pages.common.downloadLoading'), 0);
try {
const data = await func(requestData);
downloadExcelFile(data, fileName, withRandomName);
} catch (error) {
console.error(error);
} finally {
hideLoading();
}
}
export function downloadExcelFile( export function downloadExcelFile(
data: BlobPart, data: BlobPart,
filename: string, filename: string,
@ -78,6 +107,23 @@ export function downloadByData(
window.URL.revokeObjectURL(blobURL); window.URL.revokeObjectURL(blobURL);
} }
export function openWindow(
url: string,
opt?: {
noopener?: boolean;
noreferrer?: boolean;
target?: '_blank' | '_self' | string;
},
) {
const { noopener = true, noreferrer = true, target = '__blank' } = opt || {};
const feature: string[] = [];
noopener && feature.push('noopener=yes');
noreferrer && feature.push('noreferrer=yes');
window.open(url, target, feature.join(','));
}
/** /**
* Download file according to file address * Download file according to file address
* @param {*} sUrl * @param {*} sUrl

View File

@ -11,7 +11,8 @@ import { DictEnum } from '@vben/constants';
import { Space, Table } from 'ant-design-vue'; import { Space, Table } from 'ant-design-vue';
import { configList } from '#/api/system/config'; import { configExport, configList } from '#/api/system/config';
import { downloadExcel } from '#/utils/file/download';
import { renderDict } from '#/utils/render'; import { renderDict } from '#/utils/render';
import configModal from './config-modal.vue'; import configModal from './config-modal.vue';
@ -83,7 +84,10 @@ const columns: ColumnsType = [
<template> <template>
<Page> <Page>
<div class="mb-[16px] flex justify-end"> <div class="mb-[16px] flex justify-end gap-[8px]">
<a-button @click="downloadExcel(configExport, '参数配置', {})">
{{ $t('pages.common.export') }}
</a-button>
<a-button type="primary" @click="handleAdd"> <a-button type="primary" @click="handleAdd">
{{ $t('pages.common.add') }} {{ $t('pages.common.add') }}
</a-button> </a-button>