<template>
<el-button @click="handle2">click</el-button>
{{time2}}
{{time}}
</template>
<script lang="ts" setup>
import Worker2 from "@/worker?worker";
import {ref} from "vue";
const time = ref(0);
const time2 = ref(0);
let t = null;
const handle2 = async function () {
time2.value = 0;
t = setInterval(() => {
time2.value += 100;
}, 100);
const s = new Date().getTime();
const worker = new Worker2();
worker.onmessage = function ({data}) {
// debugger
console.log(data);
downloadExcelFromUint8Array(data);
clearInterval(t);
};
worker.postMessage("start");
time.value = (new Date().getTime() - s) / 1000;
};
function downloadExcelFromUint8Array(uint8Array: Uint8Array, filename = 'data.xlsx') {
// 创建 Blob 对象
const blob = new Blob([uint8Array], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
// 创建下载链接
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// 清理资源
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
</script>
<style lang="scss">
//@use "@jhlc/jh-ui/src/element-plus/index";
//@import '@jhlc/platform/lib/style.css';
</style>
worker.ts
import init, {generate_excel, generate_excel_async} from "./pkg/file_util";
self.onmessage = async () => {
await init();
const ret = await generate_excel_async(10000 * 10);
self.postMessage(ret);
}
export default {}; // 或无导出,但要确保文件会执行