From dc22b39fea31872cad95ffe85501a7d5b2eba504 Mon Sep 17 00:00:00 2001 From: icssoa <615206459@qq.com> Date: Tue, 20 Apr 2021 15:22:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20excel-export=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/crud.code-snippets | 2 +- package.json | 8 +- src/cool/index.ts | 9 +- .../modules/base/components/dept/tree.vue | 2 +- .../modules/base/components/process/index.vue | 2 +- src/cool/modules/base/views/log.vue | 2 +- src/cool/modules/base/views/menu.vue | 2 +- src/cool/modules/base/views/param.vue | 2 +- src/cool/modules/base/views/plugin.vue | 2 +- src/cool/modules/base/views/role.vue | 2 +- src/cool/modules/base/views/user.vue | 2 +- src/cool/modules/chat/components/session.vue | 2 +- src/cool/modules/crud/components/add-btn.tsx | 30 - src/cool/modules/crud/components/adv-btn.tsx | 30 - .../modules/crud/components/adv-search.tsx | 283 --------- .../components/context-menu/context-menu.tsx | 216 ------- .../crud/components/context-menu/index.ts | 16 - src/cool/modules/crud/components/crud/app.ts | 53 -- .../modules/crud/components/crud/helper.ts | 204 ------ .../modules/crud/components/crud/index.tsx | 120 ---- .../modules/crud/components/dialog/helper.ts | 139 ---- .../modules/crud/components/dialog/index.tsx | 296 --------- .../modules/crud/components/error-message.tsx | 15 - src/cool/modules/crud/components/filter.tsx | 21 - src/cool/modules/crud/components/flex1.tsx | 11 - .../modules/crud/components/form-tabs.tsx | 119 ---- .../modules/crud/components/form/helper.tsx | 131 ---- .../modules/crud/components/form/index.tsx | 517 --------------- src/cool/modules/crud/components/index.tsx | 39 -- .../crud/components/multi-delete-btn.tsx | 26 - .../modules/crud/components/pagination.tsx | 84 --- src/cool/modules/crud/components/query.tsx | 118 ---- .../modules/crud/components/refresh-btn.tsx | 23 - .../modules/crud/components/search-key.tsx | 129 ---- .../modules/crud/components/table/helper.tsx | 67 -- .../modules/crud/components/table/index.tsx | 543 ---------------- .../modules/crud/components/upsert/helper.tsx | 26 - .../modules/crud/components/upsert/index.tsx | 293 --------- src/cool/modules/crud/hooks/core.ts | 41 -- src/cool/modules/crud/hooks/form.ts | 74 --- src/cool/modules/crud/index.ts | 58 -- src/cool/modules/crud/static/index.scss | 596 ------------------ src/cool/modules/crud/types/adv-search.d.ts | 3 - src/cool/modules/crud/types/browser.d.ts | 4 - src/cool/modules/crud/types/context-menu.d.ts | 15 - src/cool/modules/crud/types/crud.d.ts | 108 ---- src/cool/modules/crud/types/form.d.ts | 84 --- src/cool/modules/crud/types/hook.ts | 14 - src/cool/modules/crud/types/index.d.ts | 10 - src/cool/modules/crud/types/op.d.ts | 111 ---- src/cool/modules/crud/types/query.d.ts | 4 - src/cool/modules/crud/types/render.d.ts | 12 - src/cool/modules/crud/types/table.d.ts | 66 -- src/cool/modules/crud/types/upsert.d.ts | 7 - src/cool/modules/crud/utils/index.ts | 115 ---- src/cool/modules/crud/utils/mitt.ts | 29 - src/cool/modules/crud/utils/parse.ts | 33 - src/cool/modules/crud/utils/vnode.tsx | 153 ----- .../demo/components/crud/adv-search.vue | 2 +- .../demo/components/crud/context-menu.vue | 2 +- .../modules/demo/components/crud/form.vue | 2 +- .../modules/demo/components/crud/query.vue | 2 +- .../modules/demo/components/crud/table.vue | 2 +- .../modules/demo/components/crud/upsert.vue | 2 +- src/cool/modules/demo/views/crud.vue | 2 +- .../excel-export/components/export-btn.vue | 147 +++++ src/cool/modules/excel-export/index.ts | 9 + .../excel-export/utils/export2excel.ts | 232 +++++++ src/cool/modules/excel-export/utils/index.ts | 16 + src/cool/modules/task/views/task.vue | 2 +- .../upload/components/space/category.vue | 4 +- .../upload/components/space/file-item.vue | 2 +- src/shims-vue.d.ts | 11 + yarn.lock | 131 +++- 74 files changed, 578 insertions(+), 5113 deletions(-) delete mode 100644 src/cool/modules/crud/components/add-btn.tsx delete mode 100644 src/cool/modules/crud/components/adv-btn.tsx delete mode 100644 src/cool/modules/crud/components/adv-search.tsx delete mode 100644 src/cool/modules/crud/components/context-menu/context-menu.tsx delete mode 100644 src/cool/modules/crud/components/context-menu/index.ts delete mode 100644 src/cool/modules/crud/components/crud/app.ts delete mode 100644 src/cool/modules/crud/components/crud/helper.ts delete mode 100644 src/cool/modules/crud/components/crud/index.tsx delete mode 100644 src/cool/modules/crud/components/dialog/helper.ts delete mode 100644 src/cool/modules/crud/components/dialog/index.tsx delete mode 100644 src/cool/modules/crud/components/error-message.tsx delete mode 100644 src/cool/modules/crud/components/filter.tsx delete mode 100644 src/cool/modules/crud/components/flex1.tsx delete mode 100644 src/cool/modules/crud/components/form-tabs.tsx delete mode 100644 src/cool/modules/crud/components/form/helper.tsx delete mode 100644 src/cool/modules/crud/components/form/index.tsx delete mode 100644 src/cool/modules/crud/components/index.tsx delete mode 100644 src/cool/modules/crud/components/multi-delete-btn.tsx delete mode 100644 src/cool/modules/crud/components/pagination.tsx delete mode 100644 src/cool/modules/crud/components/query.tsx delete mode 100644 src/cool/modules/crud/components/refresh-btn.tsx delete mode 100644 src/cool/modules/crud/components/search-key.tsx delete mode 100644 src/cool/modules/crud/components/table/helper.tsx delete mode 100644 src/cool/modules/crud/components/table/index.tsx delete mode 100644 src/cool/modules/crud/components/upsert/helper.tsx delete mode 100644 src/cool/modules/crud/components/upsert/index.tsx delete mode 100644 src/cool/modules/crud/hooks/core.ts delete mode 100644 src/cool/modules/crud/hooks/form.ts delete mode 100644 src/cool/modules/crud/index.ts delete mode 100644 src/cool/modules/crud/static/index.scss delete mode 100644 src/cool/modules/crud/types/adv-search.d.ts delete mode 100644 src/cool/modules/crud/types/browser.d.ts delete mode 100644 src/cool/modules/crud/types/context-menu.d.ts delete mode 100644 src/cool/modules/crud/types/crud.d.ts delete mode 100644 src/cool/modules/crud/types/form.d.ts delete mode 100644 src/cool/modules/crud/types/hook.ts delete mode 100644 src/cool/modules/crud/types/index.d.ts delete mode 100644 src/cool/modules/crud/types/op.d.ts delete mode 100644 src/cool/modules/crud/types/query.d.ts delete mode 100644 src/cool/modules/crud/types/render.d.ts delete mode 100644 src/cool/modules/crud/types/table.d.ts delete mode 100644 src/cool/modules/crud/types/upsert.d.ts delete mode 100644 src/cool/modules/crud/utils/index.ts delete mode 100644 src/cool/modules/crud/utils/mitt.ts delete mode 100644 src/cool/modules/crud/utils/parse.ts delete mode 100644 src/cool/modules/crud/utils/vnode.tsx create mode 100644 src/cool/modules/excel-export/components/export-btn.vue create mode 100644 src/cool/modules/excel-export/index.ts create mode 100644 src/cool/modules/excel-export/utils/export2excel.ts create mode 100644 src/cool/modules/excel-export/utils/index.ts diff --git a/.vscode/crud.code-snippets b/.vscode/crud.code-snippets index 360a9f8..9bd0526 100644 --- a/.vscode/crud.code-snippets +++ b/.vscode/crud.code-snippets @@ -34,7 +34,7 @@ "", " diff --git a/src/cool/modules/excel-export/index.ts b/src/cool/modules/excel-export/index.ts new file mode 100644 index 0000000..08f98e0 --- /dev/null +++ b/src/cool/modules/excel-export/index.ts @@ -0,0 +1,9 @@ +import ExportBtn from "./components/export-btn.vue"; + +export { ExportBtn }; + +export default { + components: { + ExportBtn + } +}; diff --git a/src/cool/modules/excel-export/utils/export2excel.ts b/src/cool/modules/excel-export/utils/export2excel.ts new file mode 100644 index 0000000..7ed7039 --- /dev/null +++ b/src/cool/modules/excel-export/utils/export2excel.ts @@ -0,0 +1,232 @@ +/* eslint-disable */ +// @ts-nocheck +import { saveAs } from 'file-saver'; +import XLSX from 'xlsx'; + +function generateArray(table) { + var out = []; + var rows = table.querySelectorAll('tr'); + var ranges = []; + for (var R = 0; R < rows.length; ++R) { + var outRow = []; + var row = rows[R]; + var columns = row.querySelectorAll('td'); + for (var C = 0; C < columns.length; ++C) { + var cell = columns[C]; + var colspan = cell.getAttribute('colspan'); + var rowspan = cell.getAttribute('rowspan'); + var cellValue = cell.innerText; + if (cellValue !== '' && cellValue == +cellValue) cellValue = +cellValue; + + //Skip ranges + ranges.forEach(function(range) { + if ( + R >= range.s.r && + R <= range.e.r && + outRow.length >= range.s.c && + outRow.length <= range.e.c + ) { + for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null); + } + }); + + //Handle Row Span + if (rowspan || colspan) { + rowspan = rowspan || 1; + colspan = colspan || 1; + ranges.push({ + s: { + r: R, + c: outRow.length + }, + e: { + r: R + rowspan - 1, + c: outRow.length + colspan - 1 + } + }); + } + + //Handle Value + outRow.push(cellValue !== '' ? cellValue : null); + + //Handle Colspan + if (colspan) for (var k = 0; k < colspan - 1; ++k) outRow.push(null); + } + out.push(outRow); + } + return [out, ranges]; +} + +function datenum(v, date1904) { + if (date1904) v += 1462; + var epoch = Date.parse(v); + return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); +} + +function sheet_from_array_of_arrays(data, opts) { + var ws = {}; + var range = { + s: { + c: 10000000, + r: 10000000 + }, + e: { + c: 0, + r: 0 + } + }; + for (var R = 0; R != data.length; ++R) { + for (var C = 0; C != data[R].length; ++C) { + if (range.s.r > R) range.s.r = R; + if (range.s.c > C) range.s.c = C; + if (range.e.r < R) range.e.r = R; + if (range.e.c < C) range.e.c = C; + var cell = { + v: data[R][C] + }; + if (cell.v == null) continue; + var cell_ref = XLSX.utils.encode_cell({ + c: C, + r: R + }); + + if (typeof cell.v === 'number') cell.t = 'n'; + else if (typeof cell.v === 'boolean') cell.t = 'b'; + else if (cell.v instanceof Date) { + cell.t = 'n'; + cell.z = XLSX.SSF._table[14]; + cell.v = datenum(cell.v); + } else cell.t = 's'; + + ws[cell_ref] = cell; + } + } + if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); + return ws; +} + +function Workbook() { + if (!(this instanceof Workbook)) return new Workbook(); + this.SheetNames = []; + this.Sheets = {}; +} + +function s2ab(s) { + var buf = new ArrayBuffer(s.length); + var view = new Uint8Array(buf); + for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xff; + return buf; +} + +export function export_table_to_excel(id) { + var theTable = document.getElementById(id); + var oo = generateArray(theTable); + var ranges = oo[1]; + + /* original data */ + var data = oo[0]; + var ws_name = 'SheetJS'; + + var wb = new Workbook(), + ws = sheet_from_array_of_arrays(data); + + /* add ranges to worksheet */ + // ws['!cols'] = ['apple', 'banan']; + ws['!merges'] = ranges; + + /* add worksheet to workbook */ + wb.SheetNames.push(ws_name); + wb.Sheets[ws_name] = ws; + + var wbout = XLSX.write(wb, { + bookType: 'xlsx', + bookSST: false, + type: 'binary' + }); + + saveAs( + new Blob([s2ab(wbout)], { + type: 'application/octet-stream' + }), + 'test.xlsx' + ); +} + +export function export_json_to_excel({ + multiHeader = [], + header, + data, + filename, + merges = [], + autoWidth = true, + bookType = 'xlsx' +} = {}) { + /* original data */ + filename = filename || 'excel-list'; + data = [...data]; + data.unshift(header); + + for (let i = multiHeader.length - 1; i > -1; i--) { + data.unshift(multiHeader[i]); + } + + var ws_name = 'SheetJS'; + var wb = new Workbook(), + ws = sheet_from_array_of_arrays(data); + + if (merges.length > 0) { + if (!ws['!merges']) ws['!merges'] = []; + merges.forEach(item => { + ws['!merges'].push(XLSX.utils.decode_range(item)); + }); + } + + if (autoWidth) { + /*设置worksheet每列的最大宽度*/ + const colWidth = data.map(row => + row.map(val => { + /*先判断是否为null/undefined*/ + if (val == null) { + return { + wch: 10 + }; + } else if (val.toString().charCodeAt(0) > 255) { + /*再判断是否为中文*/ + return { + wch: val.toString().length * 2 + }; + } else { + return { + wch: val.toString().length + }; + } + }) + ); + /*以第一行为初始值*/ + let result = colWidth[0]; + for (let i = 1; i < colWidth.length; i++) { + for (let j = 0; j < colWidth[i].length; j++) { + if (result[j]['wch'] < colWidth[i][j]['wch']) { + result[j]['wch'] = colWidth[i][j]['wch']; + } + } + } + ws['!cols'] = result; + } + + /* add worksheet to workbook */ + wb.SheetNames.push(ws_name); + wb.Sheets[ws_name] = ws; + + var wbout = XLSX.write(wb, { + bookType: bookType, + bookSST: false, + type: 'binary' + }); + saveAs( + new Blob([s2ab(wbout)], { + type: 'application/octet-stream' + }), + `${filename}.${bookType}` + ); +} diff --git a/src/cool/modules/excel-export/utils/index.ts b/src/cool/modules/excel-export/utils/index.ts new file mode 100644 index 0000000..93f1d8c --- /dev/null +++ b/src/cool/modules/excel-export/utils/index.ts @@ -0,0 +1,16 @@ +import { export_json_to_excel } from "./export2excel"; + +function currentDate() { + const d: Date = new Date(); + + return { + year: d.getFullYear(), + month: d.getMonth() + 1, + day: d.getDate(), + hour: d.getHours(), + minu: d.getMinutes(), + sec: d.getSeconds() + }; +} + +export { export_json_to_excel, currentDate }; diff --git a/src/cool/modules/task/views/task.vue b/src/cool/modules/task/views/task.vue index 06a8c62..004d65e 100644 --- a/src/cool/modules/task/views/task.vue +++ b/src/cool/modules/task/views/task.vue @@ -218,7 +218,7 @@ import { computed, defineComponent, inject, onMounted, reactive } from "vue"; import { ElMessage, ElMessageBox } from "element-plus"; import Draggable from "vuedraggable"; import { checkPerm } from "/$/base"; -import { ContextMenu } from "/$/crud"; +import { ContextMenu } from "cl-admin-crud-vue3"; import Cron from "../components/cron"; import { useRefs } from "/@/core"; diff --git a/src/cool/modules/upload/components/space/category.vue b/src/cool/modules/upload/components/space/category.vue index cd2a29a..739881e 100644 --- a/src/cool/modules/upload/components/space/category.vue +++ b/src/cool/modules/upload/components/space/category.vue @@ -37,8 +37,8 @@ import { ElMessage, ElMessageBox } from "element-plus"; import { computed, defineComponent, inject, ref, watch } from "vue"; import { useStore } from "vuex"; import { isEmpty } from "/@/core/utils"; -import { ContextMenu } from "/$/crud"; -import { useRefs } from "/$/crud/hooks/core"; +import { ContextMenu } from "cl-admin-crud-vue3"; +import { useRefs } from "/@/core"; export default defineComponent({ name: "cl-upload-space-category", diff --git a/src/cool/modules/upload/components/space/file-item.vue b/src/cool/modules/upload/components/space/file-item.vue index 0de87de..391fe11 100644 --- a/src/cool/modules/upload/components/space/file-item.vue +++ b/src/cool/modules/upload/components/space/file-item.vue @@ -47,7 +47,7 @@