From d039a6438fbd51db46e98afd35765b743b5dfa24 Mon Sep 17 00:00:00 2001 From: liyang <1711467488@qq.com> Date: Sun, 16 Dec 2018 15:39:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=A1=A8=E6=A0=BC=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=8F=90=E5=8F=96=E4=B8=BA=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E7=9A=84=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 022d07c06618417af8e2895dd00fcac3e0238c09 [formerly 022d07c06618417af8e2895dd00fcac3e0238c09 [formerly 022d07c06618417af8e2895dd00fcac3e0238c09 [formerly 022d07c06618417af8e2895dd00fcac3e0238c09 [formerly 3a6388bd7e36b1bbd3d22767b2dc3a4e462402b7 [formerly 15059226e8bd77dcbe7212699c6bab6b6ff9d385]]]]] Former-commit-id: bfa445ba0136208e94ef7244f8bf127d2b186bfd Former-commit-id: d9e3059e625371a01176c7fdcb34a25a5ea247d5 Former-commit-id: f543910387e755b14d40eec2b42eaed43c500d53 [formerly 4d3007385ebb14bf7ec659c68251af6358cc5982] Former-commit-id: 19d3ef449fa36d92f5799987d6cda668c9b3534e Former-commit-id: 91a0cb0497884cd435ff29d6b714e35b22eb9f4d Former-commit-id: 0edac75e2cf32f0001191c3b3e9829bcd081ed66 Former-commit-id: b3cfe8d5c3432d308dad9437de878f4e22008ca7 Former-commit-id: 94ec0467d510f8506c5d5e6da7aa5f377c350cb3 --- package-lock.json.REMOVED.git-id | 2 +- package.json | 7 +- src/plugin/d2admin/index.js | 4 +- src/plugin/export/_blob.js | 179 ----------------------------- src/plugin/export/_csv.js | 62 ---------- src/plugin/export/_export-csv.js | 83 ------------- src/plugin/export/_export2Excel.js | 151 ------------------------ src/plugin/export/index.js | 73 ------------ src/plugin/import/index.js | 62 ---------- 9 files changed, 6 insertions(+), 617 deletions(-) delete mode 100644 src/plugin/export/_blob.js delete mode 100755 src/plugin/export/_csv.js delete mode 100755 src/plugin/export/_export-csv.js delete mode 100644 src/plugin/export/_export2Excel.js delete mode 100644 src/plugin/export/index.js delete mode 100644 src/plugin/import/index.js diff --git a/package-lock.json.REMOVED.git-id b/package-lock.json.REMOVED.git-id index 3e323edb..97098837 100644 --- a/package-lock.json.REMOVED.git-id +++ b/package-lock.json.REMOVED.git-id @@ -1 +1 @@ -6c7225791d502ed1e41cead2c43f6f321f5ab485 \ No newline at end of file +2b614609fc8311fc059d290422304b490e9a3abc \ No newline at end of file diff --git a/package.json b/package.json index 27b58c95..96d39b4c 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,8 @@ "dependencies": { "@d2-projects/d2-crud": "^1.3.4", "@d2-projects/vue-filters-date": "^1.0.2", + "@d2-projects/vue-table-export": "^1.0.1", + "@d2-projects/vue-table-import": "^1.0.0", "axios": "^0.17.1", "babel-polyfill": "^6.26.0", "better-scroll": "^1.12.1", @@ -23,7 +25,6 @@ "dayjs": "^1.6.7", "echarts": "^4.1.0", "element-ui": "^2.4.4", - "file-saver": "^1.3.3", "flex.css": "^1.1.7", "fuse.js": "^3.2.1", "github-markdown-css": "^2.10.0", @@ -35,7 +36,6 @@ "marked": "^0.3.9", "mockjs": "^1.0.1-beta3", "nprogress": "^0.2.0", - "papaparse": "^4.3.6", "qs": "^6.6.0", "quill": "^1.3.4", "screenfull": "^3.3.2", @@ -50,8 +50,7 @@ "vue-router": "^3.0.1", "vue-splitpane": "^1.0.2", "vue-ueditor-wrap": "^1.5.0", - "vuex": "^3.0.1", - "xlsx": "^0.12.0" + "vuex": "^3.0.1" }, "devDependencies": { "@kazupon/vue-i18n-loader": "^0.3.0", diff --git a/src/plugin/d2admin/index.js b/src/plugin/d2admin/index.js index a552f8ec..329d2ebf 100644 --- a/src/plugin/d2admin/index.js +++ b/src/plugin/d2admin/index.js @@ -11,8 +11,8 @@ import '@/assets/svg-icons' import d2VueFiltersDate from '@d2-projects/vue-filters-date' // 功能插件 import pluginError from '@/plugin/error' -import pluginExport from '@/plugin/export' -import pluginImport from '@/plugin/import' +import pluginExport from '@d2-projects/vue-table-export' +import pluginImport from '@d2-projects/vue-table-import' import pluginLog from '@/plugin/log' import pluginOpen from '@/plugin/open' diff --git a/src/plugin/export/_blob.js b/src/plugin/export/_blob.js deleted file mode 100644 index 26382ccd..00000000 --- a/src/plugin/export/_blob.js +++ /dev/null @@ -1,179 +0,0 @@ -/* eslint-disable */ -/* Blob.js - * A Blob implementation. - * 2014-05-27 - * - * By Eli Grey, http://eligrey.com - * By Devin Samarin, https://github.com/eboyjr - * License: X11/MIT - * See LICENSE.md - */ - -/*global self, unescape */ -/*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true, - plusplus: true */ - -/*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */ - -(function (view) { - "use strict"; - - view.URL = view.URL || view.webkitURL; - - if (view.Blob && view.URL) { - try { - new Blob; - return; - } catch (e) {} - } - - // Internally we use a BlobBuilder implementation to base Blob off of - // in order to support older browsers that only have BlobBuilder - var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) { - var - get_class = function(object) { - return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1]; - } - , FakeBlobBuilder = function BlobBuilder() { - this.data = []; - } - , FakeBlob = function Blob(data, type, encoding) { - this.data = data; - this.size = data.length; - this.type = type; - this.encoding = encoding; - } - , FBB_proto = FakeBlobBuilder.prototype - , FB_proto = FakeBlob.prototype - , FileReaderSync = view.FileReaderSync - , FileException = function(type) { - this.code = this[this.name = type]; - } - , file_ex_codes = ( - "NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR " - + "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR" - ).split(" ") - , file_ex_code = file_ex_codes.length - , real_URL = view.URL || view.webkitURL || view - , real_create_object_URL = real_URL.createObjectURL - , real_revoke_object_URL = real_URL.revokeObjectURL - , URL = real_URL - , btoa = view.btoa - , atob = view.atob - - , ArrayBuffer = view.ArrayBuffer - , Uint8Array = view.Uint8Array - ; - FakeBlob.fake = FB_proto.fake = true; - while (file_ex_code--) { - FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1; - } - if (!real_URL.createObjectURL) { - URL = view.URL = {}; - } - URL.createObjectURL = function(blob) { - var - type = blob.type - , data_URI_header - ; - if (type === null) { - type = "application/octet-stream"; - } - if (blob instanceof FakeBlob) { - data_URI_header = "data:" + type; - if (blob.encoding === "base64") { - return data_URI_header + ";base64," + blob.data; - } else if (blob.encoding === "URI") { - return data_URI_header + "," + decodeURIComponent(blob.data); - } if (btoa) { - return data_URI_header + ";base64," + btoa(blob.data); - } else { - return data_URI_header + "," + encodeURIComponent(blob.data); - } - } else if (real_create_object_URL) { - return real_create_object_URL.call(real_URL, blob); - } - }; - URL.revokeObjectURL = function(object_URL) { - if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) { - real_revoke_object_URL.call(real_URL, object_URL); - } - }; - FBB_proto.append = function(data/*, endings*/) { - var bb = this.data; - // decode data to a binary string - if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) { - var - str = "" - , buf = new Uint8Array(data) - , i = 0 - , buf_len = buf.length - ; - for (; i < buf_len; i++) { - str += String.fromCharCode(buf[i]); - } - bb.push(str); - } else if (get_class(data) === "Blob" || get_class(data) === "File") { - if (FileReaderSync) { - var fr = new FileReaderSync; - bb.push(fr.readAsBinaryString(data)); - } else { - // async FileReader won't work as BlobBuilder is sync - throw new FileException("NOT_READABLE_ERR"); - } - } else if (data instanceof FakeBlob) { - if (data.encoding === "base64" && atob) { - bb.push(atob(data.data)); - } else if (data.encoding === "URI") { - bb.push(decodeURIComponent(data.data)); - } else if (data.encoding === "raw") { - bb.push(data.data); - } - } else { - if (typeof data !== "string") { - data += ""; // convert unsupported types to strings - } - // decode UTF-16 to binary string - bb.push(unescape(encodeURIComponent(data))); - } - }; - FBB_proto.getBlob = function(type) { - if (!arguments.length) { - type = null; - } - return new FakeBlob(this.data.join(""), type, "raw"); - }; - FBB_proto.toString = function() { - return "[object BlobBuilder]"; - }; - FB_proto.slice = function(start, end, type) { - var args = arguments.length; - if (args < 3) { - type = null; - } - return new FakeBlob( - this.data.slice(start, args > 1 ? end : this.data.length) - , type - , this.encoding - ); - }; - FB_proto.toString = function() { - return "[object Blob]"; - }; - FB_proto.close = function() { - this.size = this.data.length = 0; - }; - return FakeBlobBuilder; - }(view)); - - view.Blob = function Blob(blobParts, options) { - var type = options ? (options.type || "") : ""; - var builder = new BlobBuilder(); - if (blobParts) { - for (var i = 0, len = blobParts.length; i < len; i++) { - builder.append(blobParts[i]); - } - } - return builder.getBlob(type); - }; -}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this)); diff --git a/src/plugin/export/_csv.js b/src/plugin/export/_csv.js deleted file mode 100755 index de2796cb..00000000 --- a/src/plugin/export/_csv.js +++ /dev/null @@ -1,62 +0,0 @@ -/* - inspired by https://www.npmjs.com/package/react-csv-downloader - now removed from Github - inspired by https://github.com/iview/iview -*/ - -/* eslint-disable */ - -const newLine = '\r\n'; -const appendLine = (content, row, { separator, quoted }) => { - const line = row.map(data => { - if (!quoted) return data; - // quote data - data = typeof data === 'string' ? data.replace(/"/g, '"') : data; - return `"${data}"`; - }); - content.push(line.join(separator)); -}; - -const defaults = { - separator: ',', - quoted: false -}; - -export default function csv(columns, datas, options, noHeader = false) { - options = Object.assign({}, defaults, options); - let columnOrder; - const content = []; - const column = []; - - if (columns) { - columnOrder = columns.map(v => { - if (typeof v === 'string') return v; - if (!noHeader) { - column.push(typeof v.label !== 'undefined' ? v.label : v.prop); - } - return v.prop; - }); - if (column.length > 0) appendLine(content, column, options); - } else { - columnOrder = []; - datas.forEach(v => { - if (!Array.isArray(v)) { - columnOrder = columnOrder.concat(Object.keys(v)); - } - }); - if (columnOrder.length > 0) { - columnOrder = columnOrder.filter((value, index, self) => self.indexOf(value) === index); - if (!noHeader) appendLine(content, columnOrder, options); - } - } - - if (Array.isArray(datas)) { - datas.forEach(row => { - if (!Array.isArray(row)) { - row = columnOrder.map(k => (typeof row[k] !== 'undefined' ? row[k] : '')); - } - appendLine(content, row, options); - }); - } - return content.join(newLine); -} diff --git a/src/plugin/export/_export-csv.js b/src/plugin/export/_export-csv.js deleted file mode 100755 index 50caf2af..00000000 --- a/src/plugin/export/_export-csv.js +++ /dev/null @@ -1,83 +0,0 @@ -/* eslint-disable */ - -/* - 此代码来源于iview表格组件的CSV导出部分 - https://github.com/iview/iview -*/ - -function has (browser) { - const ua = navigator.userAgent; - if (browser === 'ie') { - const isIE = ua.indexOf('compatible') > -1 && ua.indexOf('MSIE') > -1; - if (isIE) { - const reIE = new RegExp('MSIE (\\d+\\.\\d+);'); - reIE.test(ua); - return parseFloat(RegExp['$1']); - } else { - return false; - } - } else { - return ua.indexOf(browser) > -1; - } -} - -const csv = { - _isIE11 () { - let iev = 0; - const ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent)); - const trident = !!navigator.userAgent.match(/Trident\/7.0/); - const rv = navigator.userAgent.indexOf('rv:11.0'); - - if (ieold) { - iev = Number(RegExp.$1); - } - if (navigator.appVersion.indexOf('MSIE 10') !== -1) { - iev = 10; - } - if (trident && rv !== -1) { - iev = 11; - } - - return iev === 11; - }, - - _isEdge () { - return /Edge/.test(navigator.userAgent); - }, - - _getDownloadUrl (text) { - const BOM = '\uFEFF'; - // Add BOM to text for open in excel correctly - if (window.Blob && window.URL && window.URL.createObjectURL) { - const csvData = new Blob([BOM + text], { type: 'text/csv' }); - return URL.createObjectURL(csvData); - } else { - return 'data:attachment/csv;charset=utf-8,' + BOM + encodeURIComponent(text); - } - }, - - download (filename, text) { - if (has('ie') && has('ie') < 10) { - // has module unable identify ie11 and Edge - const oWin = window.top.open('about:blank', '_blank'); - oWin.document.charset = 'utf-8'; - oWin.document.write(text); - oWin.document.close(); - oWin.document.execCommand('SaveAs', filename + '.csv'); - oWin.close(); - } else if (has('ie') === 10 || this._isIE11() || this._isEdge()) { - const BOM = '\uFEFF'; - const csvData = new Blob([BOM + text], { type: 'text/csv' }); - navigator.msSaveBlob(csvData, filename + '.csv'); - } else { - const link = document.createElement('a'); - link.download = filename + '.csv'; - link.href = this._getDownloadUrl(text); - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - } - } -}; - -export default csv; \ No newline at end of file diff --git a/src/plugin/export/_export2Excel.js b/src/plugin/export/_export2Excel.js deleted file mode 100644 index 0efa20ef..00000000 --- a/src/plugin/export/_export2Excel.js +++ /dev/null @@ -1,151 +0,0 @@ -/* eslint-disable */ - -// 来源于网络 有细微改动 - -import './_blob' -import FileSaver 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); - console.log('a') - var oo = generateArray(theTable); - var ranges = oo[1]; - - /* original data */ - var data = oo[0]; - var ws_name = "SheetJS"; - console.log(data); - - 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'}); - - FileSaver.saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), "test.xlsx") -} - -function formatJson(jsonData) { - console.log(jsonData) -} -export function export_json_to_excel(th, jsonData, defaultTitle, options = { merges: [], header: null }) { - - /* original data */ - - var data = jsonData; - data.unshift(th); - if (options.header) data.unshift([options.header]); - var ws_name = "SheetJS"; - - var wb = new Workbook(), ws = sheet_from_array_of_arrays(data); - - /* add merges area to worksheet */ - let { merges } = options; - if (typeof merges[0] == 'string' && merges.length == 2) merges = [merges] // just one # ['A1', 'C1'] = > [['A1', 'C1']] - merges = merges.map(i => i instanceof Array ? { s: i[0], e: i[1] } : i); // be sort :) # ['A1', 'C1'] => { s: 'A1', e: 'C3' } - ws['!merges'] = merges; - - /* 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'}); - var title = defaultTitle || '列表' - FileSaver.saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx") -} diff --git a/src/plugin/export/index.js b/src/plugin/export/index.js deleted file mode 100644 index 3449897b..00000000 --- a/src/plugin/export/index.js +++ /dev/null @@ -1,73 +0,0 @@ -/* eslint-disable */ - -// 库 -import Csv from './_csv' -import ExportCsv from './_export-csv' -import FileSaver from 'file-saver' -import * as Excel from './_export2Excel' - -export default { - install (Vue, options) { - Vue.prototype.$export = { - // 导出 csv - csv (params) { - return new Promise((resolve, reject) => { - // 默认值 - const paramsDefault = { - columns: [], - data: [], - title: 'table', - noHeader: false - } - // 合并参数 - const _params = Object.assign({}, paramsDefault, params) - // 生成数据 - const data = Csv(_params.columns, _params.data, params, _params.noHeader) - // 下载数据 - ExportCsv.download(_params.title, data) - // 完成 - resolve() - }) - }, - // 导出 excel - excel (params) { - return new Promise((resolve, reject) => { - // 默认值 - const paramsDefault = { - columns: [], - data: [], - title: 'table', - header: null, - merges: [] - } - // 合并参数 - const _params = Object.assign({}, paramsDefault, params) - // 从参数中派生数据 - const header = _params.columns.map(e => e.label) - const data = _params.data.map(row => _params.columns.map(col => row[col.prop])) - // 导出 - Excel.export_json_to_excel(header, data, _params.title, { merges: _params.merges, header: _params.header }) - // 完成 - resolve() - }) - }, - // 导出 文本文档 - txt (params) { - return new Promise((resolve, reject) => { - // 默认值 - const paramsDefault = { - text: '', - title: '文本' - } - // 合并参数 - const _params = Object.assign({}, paramsDefault, params) - // 导出 - const blob = new Blob([_params.text], {type: 'text/plain;charset=utf-8'}) - FileSaver.saveAs(blob, _params.title + '.txt') - // 完成 - resolve() - }) - } - } - } -} diff --git a/src/plugin/import/index.js b/src/plugin/import/index.js deleted file mode 100644 index 467e4eaa..00000000 --- a/src/plugin/import/index.js +++ /dev/null @@ -1,62 +0,0 @@ -/* eslint-disable */ - -// 库 -import papa from 'papaparse' -import xlsx from 'xlsx' - -export default { - install (Vue, options) { - Vue.prototype.$import = { - // 导入 csv - csv (file) { - return new Promise((resolve, reject) => { - papa.parse(file, { - header: true, - skipEmptyLines: true, - complete: (results, file) => { - resolve(results) - } - }) - }) - }, - // 导入 xlsx - xlsx (file) { - return new Promise((resolve, reject) => { - const reader = new FileReader() - const fixdata = data => { - let o = '' - let l = 0 - const w = 10240 - for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w))) - o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w))) - return o - } - const getHeaderRow = sheet => { - const headers = [] - const range = xlsx.utils.decode_range(sheet['!ref']) - let C - const R = range.s.r - for (C = range.s.c; C <= range.e.c; ++C) { - var cell = sheet[xlsx.utils.encode_cell({ c: C, r: R })] - var hdr = 'UNKNOWN ' + C - if (cell && cell.t) hdr = xlsx.utils.format_cell(cell) - headers.push(hdr) - } - return headers - } - reader.onload = e => { - const data = e.target.result - const fixedData = fixdata(data) - const workbook = xlsx.read(btoa(fixedData), { type: 'base64' }) - const firstSheetName = workbook.SheetNames[0] - const worksheet = workbook.Sheets[firstSheetName] - const header = getHeaderRow(worksheet) - const results = xlsx.utils.sheet_to_json(worksheet) - resolve({header, results}) - } - reader.readAsArrayBuffer(file) - }) - } - } - } -}