chore: 移除 xlsx 依赖, 重构 table 导出
This commit is contained in:
@@ -128,4 +128,4 @@ watch(
|
||||
></div>
|
||||
<slot></slot>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
@@ -5,7 +5,6 @@ export default {
|
||||
</script>
|
||||
|
||||
<script setup lang="ts">
|
||||
import * as XLSX from "xlsx";
|
||||
import { ref, watch, useSlots, withDefaults, onMounted } from "vue";
|
||||
import { v4 as uuidv4 } from "../../utils/guidUtil";
|
||||
import { Recordable } from "../../types";
|
||||
@@ -120,29 +119,43 @@ const print = function () {
|
||||
|
||||
// 导出 table 数据
|
||||
const exportData = () => {
|
||||
const wb = XLSX.utils.book_new();
|
||||
let arr: any[] = [];
|
||||
props.dataSource.forEach((item) => {
|
||||
let obj = {};
|
||||
const head = [];
|
||||
const body = [];
|
||||
tableColumns.value.forEach(item => {
|
||||
head.push(item.title);
|
||||
})
|
||||
tableDataSource.value.forEach((item) => {
|
||||
let obj = [];
|
||||
tableColumns.value.forEach((tableColumn) => {
|
||||
// @ts-ignore
|
||||
Object.keys(item).forEach((name) => {
|
||||
if (tableColumn.key === name) {
|
||||
// @ts-ignore
|
||||
obj[tableColumn.title] = item[name];
|
||||
obj.push(item[name]);
|
||||
}
|
||||
});
|
||||
});
|
||||
arr.push(obj);
|
||||
});
|
||||
const ws = XLSX.utils.json_to_sheet(arr);
|
||||
XLSX.utils.book_append_sheet(wb, ws, "sheet");
|
||||
XLSX.writeFile(wb, "export.xls", {
|
||||
bookType: "xls",
|
||||
body.push(obj);
|
||||
});
|
||||
exportToExcel(head, body)
|
||||
return;
|
||||
};
|
||||
|
||||
function exportToExcel(headerList, bodyList){
|
||||
let excelList = [];
|
||||
excelList.push(headerList.join("\t,"));
|
||||
excelList.push("\n");
|
||||
bodyList.forEach(item=>{
|
||||
excelList.push(item.join("\t,"));
|
||||
excelList.push("\n");
|
||||
})
|
||||
var merged = excelList .join("");
|
||||
let link = document.createElement("a");
|
||||
link.href = "data:text/xls;charset=utf-8,\ufeff" + encodeURIComponent(merged);
|
||||
link.download = `table.xls`;
|
||||
link.click();
|
||||
}
|
||||
|
||||
const sortTable = (e: any, key: string, sort: string) => {
|
||||
// 当前排序
|
||||
let currentSort = e.target.parentNode.getAttribute("lay-sort");
|
||||
|
||||
Reference in New Issue
Block a user