This commit is contained in:
xuliangzhan
2020-02-19 11:36:48 +08:00
parent de6afac182
commit 8824724280
5 changed files with 29 additions and 43 deletions

18
dist/index.common.js vendored

File diff suppressed because one or more lines are too long

18
dist/index.js vendored
View File

@@ -10,7 +10,7 @@
factory(mod.exports, global.XEUtils, global.XLSX);
global.VXETablePluginExportXLSX = mod.exports.default;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _xeUtils, _xlsx) {
})(this, function (_exports, _xeUtils, _xlsx) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
@@ -22,16 +22,10 @@
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function getSeq($table, row, rowIndex, column, columnIndex) {
// 在 v3.0 中废弃 startIndex、indexMethod
var seqOpts = $table.seqOpts;
var seqMethod = seqOpts.seqMethod || column.indexMethod;
return seqMethod ? seqMethod({
row: row,
rowIndex: rowIndex,
column: column,
columnIndex: columnIndex
}) : (seqOpts.startIndex || $table.startIndex) + rowIndex + 1;
function getFooterCellValue($table, opts, rows, column) {
var cellValue = _xeUtils["default"].toString(rows[$table.$getColumnIndex(column)]);
return cellValue;
}
function toBuffer(wbout) {
@@ -73,7 +67,7 @@
footers.forEach(function (rows) {
var item = {};
columns.forEach(function (column) {
item[column.id] = rows[$table.$getColumnIndex(column)] || '';
item[column.id] = getFooterCellValue($table, options, rows, column);
});
footList.push(item);
});

2
dist/index.min.js vendored
View File

@@ -1 +1 @@
!function(e,t){if("function"==typeof define&&define.amd)define("vxe-table-plugin-export-xlsx",["exports","xe-utils","xlsx"],t);else if("undefined"!=typeof exports)t(exports,require("xe-utils"),require("xlsx"));else{var o={exports:{}};t(o.exports,e.XEUtils,e.XLSX),e.VXETablePluginExportXLSX=o.exports.default}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,function(e,h,y){"use strict";function t(e){return e&&e.__esModule?e:{default:e}}function o(e){var n=e.$table,t=e.options,a=e.columns,o=e.datas,r=t.sheetName,i=t.type,s=t.isHeader,l=t.isFooter,u=t.original,f=t.message,c=t.footerFilterMethod,d={},p=[],v=o;if(s&&a.forEach(function(e){d[e.id]=h.default.toString(u?e.property:e.getTitle())}),l){var x=n.footerData;(c?x.filter(c):x).forEach(function(t){var o={};a.forEach(function(e){o[e.id]=t[n.$getColumnIndex(e)]||""}),p.push(o)})}var m=y.default.utils.book_new(),b=y.default.utils.json_to_sheet((s?[d]:[]).concat(v).concat(p),{skipHeader:!0});y.default.utils.book_append_sheet(m,b,r);var g=y.default.write(m,{bookType:i,bookSST:!1,type:"binary"});!function(e,t){if(window.Blob){var o=t.filename,n=t.type;if(navigator.msSaveBlob)navigator.msSaveBlob(e,"".concat(o,".").concat(n));else{var a=document.createElement("a");a.target="_blank",a.download="".concat(o,".").concat(n),a.href=URL.createObjectURL(e),document.body.appendChild(a),a.click(),document.body.removeChild(a)}}else console.error(w("vxe.error.notExp"))}(new Blob([function(e){for(var t=new ArrayBuffer(e.length),o=new Uint8Array(t),n=0;n!==e.length;++n)o[n]=255&e.charCodeAt(n);return t}(g)],{type:"application/octet-stream"}),t),!1!==f&&n.$XModal.message({message:w("vxe.table.expSuccess"),status:"success"})}function p(e){return e.replace(/^"/,"").replace(/"$/,"")}function n(e){var l=e.$table,u=e.columns,f=e.options,t=e.file,c=l._importCallback,d=l._importResolve,o=new FileReader;o.onload=function(e){var t,o,n=y.default.read(e.target.result,{type:"binary"}),a=function(e){var t=e.split("\n"),n=[],a=[];if(t.length){var o=t.slice(1);t[0].split(",").map(p),o.forEach(function(e){if(e){var o={};e.split(",").forEach(function(e,t){n[t]&&(o[n[t]]=p(e))}),a.push(o)}})}return{fields:n,rows:a}}(y.default.utils.sheet_to_csv(n.Sheets.Sheet1)),r=a.fields,i=a.rows,s=(t=r,o=[],u.forEach(function(e){var t=e.property;t&&o.push(t)}),o.every(function(e){return t.includes(e)}));s?(l.createData(i).then(function(e){"append"===f.mode?l.insertAt(e,-1):l.reloadData(e)}),!1!==f.message&&l.$XModal.message({message:w("vxe.table.impSuccess"),status:"success"})):!1!==f.message&&l.$XModal.message({message:w("vxe.error.impFields"),status:"error"}),d?(d(s),l._importResolve=null):c&&(c(s),l._importCallback=null)},o.readAsBinaryString(t)}function a(e){if("xlsx"===e.options.type)return n(e),!1}function r(e){if("xlsx"===e.options.type)return o(e),!1}Object.defineProperty(e,"__esModule",{value:!0}),e.default=e.VXETablePluginExportXLSX=void 0,h=t(h),y=t(y);var i={install:function(e){Object.assign(e.types,{xlsx:1}),e.interceptor.mixin({"event.import":a,"event.export":r}),i.t=e.t}};function w(e){if(i.t)return i.t(e)}e.VXETablePluginExportXLSX=i,"undefined"!=typeof window&&window.VXETable&&window.VXETable.use(i);var s=i;e.default=s});
!function(e,t){if("function"==typeof define&&define.amd)define("vxe-table-plugin-export-xlsx",["exports","xe-utils","xlsx"],t);else if("undefined"!=typeof exports)t(exports,require("xe-utils"),require("xlsx"));else{var n={exports:{}};t(n.exports,e.XEUtils,e.XLSX),e.VXETablePluginExportXLSX=n.exports.default}}(this,function(e,h,y){"use strict";function t(e){return e&&e.__esModule?e:{default:e}}function n(e){var o=e.$table,t=e.options,r=e.columns,n=e.datas,a=t.sheetName,i=t.type,s=t.isHeader,l=t.isFooter,u=t.original,c=t.message,f=t.footerFilterMethod,d={},p=[],v=n;if(s&&r.forEach(function(e){d[e.id]=h.default.toString(u?e.property:e.getTitle())}),l){var x=o.footerData;(f?x.filter(f):x).forEach(function(t){var n={};r.forEach(function(e){n[e.id]=function(e,t,n,o){return h.default.toString(n[e.$getColumnIndex(o)])}(o,0,t,e)}),p.push(n)})}var m=y.default.utils.book_new(),b=y.default.utils.json_to_sheet((s?[d]:[]).concat(v).concat(p),{skipHeader:!0});y.default.utils.book_append_sheet(m,b,a);var g=y.default.write(m,{bookType:i,bookSST:!1,type:"binary"});!function(e,t){if(window.Blob){var n=t.filename,o=t.type;if(navigator.msSaveBlob)navigator.msSaveBlob(e,"".concat(n,".").concat(o));else{var r=document.createElement("a");r.target="_blank",r.download="".concat(n,".").concat(o),r.href=URL.createObjectURL(e),document.body.appendChild(r),r.click(),document.body.removeChild(r)}}else console.error(w("vxe.error.notExp"))}(new Blob([function(e){for(var t=new ArrayBuffer(e.length),n=new Uint8Array(t),o=0;o!==e.length;++o)n[o]=255&e.charCodeAt(o);return t}(g)],{type:"application/octet-stream"}),t),!1!==c&&o.$XModal.message({message:w("vxe.table.expSuccess"),status:"success"})}function f(e){return e.replace(/^"/,"").replace(/"$/,"")}function o(e){var i=e.$table,s=e.columns,l=e.options,t=e.file,u=i._importCallback,c=i._importResolve,n=new FileReader;n.onload=function(e){var t=y.default.read(e.target.result,{type:"binary"}),n=function(e,t){var n=t.split("\n"),o=[],r=[];if(n.length){var a=n.slice(1);n[0].split(",").map(f),a.forEach(function(e){if(e){var n={};e.split(",").forEach(function(e,t){o[t]&&(n[o[t]]=f(e))}),r.push(n)}})}return{fields:o,rows:r}}(0,y.default.utils.sheet_to_csv(t.Sheets.Sheet1)),o=n.fields,r=n.rows,a=function(e,t){var n=[];return e.forEach(function(e){var t=e.property;t&&n.push(t)}),n.every(function(e){return t.includes(e)})}(s,o);a?(i.createData(r).then(function(e){"append"===l.mode?i.insertAt(e,-1):i.reloadData(e)}),!1!==l.message&&i.$XModal.message({message:w("vxe.table.impSuccess"),status:"success"})):!1!==l.message&&i.$XModal.message({message:w("vxe.error.impFields"),status:"error"}),c?(c(a),i._importResolve=null):u&&(u(a),i._importCallback=null)},n.readAsBinaryString(t)}function r(e){if("xlsx"===e.options.type)return o(e),!1}function a(e){if("xlsx"===e.options.type)return n(e),!1}Object.defineProperty(e,"__esModule",{value:!0}),e.default=e.VXETablePluginExportXLSX=void 0,h=t(h),y=t(y);var i={install:function(e){Object.assign(e.types,{xlsx:1}),e.interceptor.mixin({"event.import":r,"event.export":a}),i.t=e.t}};function w(e){if(i.t)return i.t(e)}e.VXETablePluginExportXLSX=i,"undefined"!=typeof window&&window.VXETable&&window.VXETable.use(i);var s=i;e.default=s});

View File

@@ -2,21 +2,19 @@ import XEUtils from 'xe-utils/methods/xe-utils'
import VXETable from 'vxe-table/lib/vxe-table'
import XLSX from 'xlsx'
function getSeq($table: any, row: any, rowIndex: number, column: any, columnIndex: number) {
// 在 v3.0 中废弃 startIndex、indexMethod
let seqOpts = $table.seqOpts
let seqMethod = seqOpts.seqMethod || column.indexMethod
return seqMethod ? seqMethod({ row, rowIndex, column, columnIndex }) : ((seqOpts.startIndex || $table.startIndex) + rowIndex + 1)
function getFooterCellValue ($table: any, opts: any, rows: any[], column: any) {
var cellValue = XEUtils.toString(rows[$table.$getColumnIndex(column)])
return cellValue
}
function toBuffer(wbout: any) {
function toBuffer (wbout: any) {
let buf = new ArrayBuffer(wbout.length)
let view = new Uint8Array(buf)
for (let index = 0; index !== wbout.length; ++index) view[index] = wbout.charCodeAt(index) & 0xFF
return buf
}
function exportXLSX(params: any) {
function exportXLSX (params: any) {
const { $table, options, columns, datas } = params
const { sheetName, type, isHeader, isFooter, original, message, footerFilterMethod } = options
const colHead: any = {}
@@ -33,7 +31,7 @@ function exportXLSX(params: any) {
footers.forEach((rows: any[]) => {
const item: any = {}
columns.forEach((column: any) => {
item[column.id] = rows[$table.$getColumnIndex(column)] || ''
item[column.id] = getFooterCellValue($table, options, rows, column)
})
footList.push(item)
})
@@ -51,7 +49,7 @@ function exportXLSX(params: any) {
}
}
function downloadFile(blob: Blob, options: any) {
function downloadFile (blob: Blob, options: any) {
if (window.Blob) {
const { filename, type } = options
if (navigator.msSaveBlob) {
@@ -70,11 +68,11 @@ function downloadFile(blob: Blob, options: any) {
}
}
function replaceDoubleQuotation(val: string) {
function replaceDoubleQuotation (val: string) {
return val.replace(/^"/, '').replace(/"$/, '')
}
function parseCsv(columns: any[], content: string) {
function parseCsv (columns: any[], content: string) {
const list: string[] = content.split('\n')
const fields: any[] = []
const rows: any[] = []
@@ -96,7 +94,7 @@ function parseCsv(columns: any[], content: string) {
return { fields, rows }
}
function checkImportData(columns: any[], fields: string[], rows: any[]) {
function checkImportData (columns: any[], fields: string[], rows: any[]) {
let tableFields: string[] = []
columns.forEach((column: any) => {
let field: string = column.property
@@ -107,7 +105,7 @@ function checkImportData(columns: any[], fields: string[], rows: any[]) {
return tableFields.every((field: string) => fields.includes(field))
}
function importXLSX(params: any) {
function importXLSX (params: any) {
const { $table, columns, options, file } = params
const { _importCallback, _importResolve } = $table
const fileReader = new FileReader()
@@ -144,14 +142,14 @@ function importXLSX(params: any) {
fileReader.readAsBinaryString(file)
}
function handleImportEvent(params: any) {
function handleImportEvent (params: any) {
if (params.options.type === 'xlsx') {
importXLSX(params)
return false
}
}
function handleExportEvent(params: any) {
function handleExportEvent (params: any) {
if (params.options.type === 'xlsx') {
exportXLSX(params)
return false
@@ -162,7 +160,7 @@ function handleExportEvent(params: any) {
* 基于 vxe-table 表格的增强插件,支持导出 xlsx 格式
*/
export const VXETablePluginExportXLSX: any = {
install(xtable: typeof VXETable) {
install (xtable: typeof VXETable) {
Object.assign(xtable.types, { xlsx: 1 })
xtable.interceptor.mixin({
'event.import': handleImportEvent,
@@ -172,7 +170,7 @@ export const VXETablePluginExportXLSX: any = {
}
}
function i18n(key: string) {
function i18n (key: string) {
if (VXETablePluginExportXLSX.t) {
return VXETablePluginExportXLSX.t(key)
}

View File

@@ -1,6 +1,6 @@
{
"name": "vxe-table-plugin-export-xlsx",
"version": "1.2.0",
"version": "1.2.1",
"description": "基于 vxe-table 表格的增强插件,支持导出 xlsx 格式",
"scripts": {
"lib": "gulp build"