1
0
mirror of synced 2025-11-06 04:10:39 +08:00

支持 xlsx 导出

This commit is contained in:
xuliangzhan
2019-11-04 21:26:13 +08:00
commit d5ce1c3c21
12 changed files with 541 additions and 0 deletions

31
.eslintrc.js Normal file
View File

@@ -0,0 +1,31 @@
// https://eslint.org/docs/user-guide/configuring
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {
impliedStrict: true,
objectLiteralDuplicateProperties: false
}
},
env: {
amd: true,
browser: true,
node: true
},
plugins: [
'@typescript-eslint'
],
extends: [
'prettier/@typescript-eslint',
'standard'
],
rules: {
'array-bracket-spacing': ['error', 'never'],
'no-debugger': ['error'],
'keyword-spacing': ['error']
}
}

21
.gitignore vendored Normal file
View File

@@ -0,0 +1,21 @@
.DS_Store
node_modules
package-lock.json
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Xu Liangzhan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

28
README.md Normal file
View File

@@ -0,0 +1,28 @@
# vxe-table-plugin-export
[![gitee star](https://gitee.com/xuliangzhan_admin/vxe-table-plugin-export/badge/star.svg?theme=dark)](https://gitee.com/xuliangzhan_admin/vxe-table-plugin-export/stargazers)
[![npm version](https://img.shields.io/npm/v/vxe-table-plugin-export.svg?style=flat-square)](https://www.npmjs.org/package/vxe-table-plugin-export)
[![npm downloads](https://img.shields.io/npm/dm/vxe-table-plugin-export.svg?style=flat-square)](http://npm-stat.com/charts.html?package=vxe-table-plugin-export)
[![gzip size: JS](http://img.badgesize.io/https://unpkg.com/vxe-table-plugin-export/dist/index.min.js?compression=gzip&label=gzip%20size:%20JS)](https://unpkg.com/vxe-table-plugin-export/dist/index.min.js)
[![npm license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/xuliangzhan/vxe-table-plugin-export/blob/master/LICENSE)
基于 [vxe-table](https://github.com/xuliangzhan/vxe-table) 表格的增强插件,支持导出 xlsx 等格式
## Installing
```shell
npm install xe-utils vxe-table vxe-table-plugin-export xlsx file-saver
```
```javascript
import Vue from 'vue'
import VXETable from 'vxe-table'
import VXETablePluginExport from 'vxe-table-plugin-export'
Vue.use(VXETable)
VXETable.use(VXETablePluginExport)
```
## License
MIT License, 2019-present, Xu Liangzhan

4
depend.ts Normal file
View File

@@ -0,0 +1,4 @@
import XEUtils from 'xe-utils/methods/xe-utils'
import dependencies from 'vxe-table/lib/utils/dependencies'
XEUtils.mixin(dependencies)

100
dist/index.common.js vendored Normal file

File diff suppressed because one or more lines are too long

110
dist/index.js vendored Normal file
View File

@@ -0,0 +1,110 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define("vxe-table-plugin-export", ["exports", "xe-utils", "xlsx", "file-saver"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("xe-utils"), require("xlsx"), require("file-saver"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.XEUtils, global.xlsx, global.fileSaver);
global.VXETablePluginExport = mod.exports.default;
}
})(this, function (_exports, _xeUtils, XLSX, FileSaver) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports["default"] = _exports.VXETablePluginExport = void 0;
_xeUtils = _interopRequireDefault(_xeUtils);
XLSX = _interopRequireWildcard(XLSX);
FileSaver = _interopRequireWildcard(FileSaver);
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function toBuffer(wbout) {
var buf = new ArrayBuffer(wbout.length);
var view = new Uint8Array(buf);
for (var index = 0; index !== wbout.length; ++index) {
view[index] = wbout.charCodeAt(index) & 0xFF;
}
return buf;
}
function toXLSX(params) {
var options = params.options,
columns = params.columns,
datas = params.datas;
var filename = options.filename,
type = options.type,
isHeader = options.isHeader,
original = options.original;
var colHead = {};
if (isHeader) {
columns.forEach(function (column) {
colHead[column.id] = column.getTitle();
});
}
var rowList = datas.map(function (row) {
var item = {};
columns.forEach(function (column) {
item[column.id] = original ? _xeUtils["default"].get(row, column.property) : row[column.id];
});
return item;
});
var book = XLSX.utils.book_new();
var sheet = XLSX.utils.json_to_sheet((isHeader ? [colHead] : []).concat(rowList), {
skipHeader: true
}); // 转换数据
XLSX.utils.book_append_sheet(book, sheet, filename);
var wbout = XLSX.write(book, {
bookType: type,
bookSST: false,
type: 'binary'
});
var blob = new Blob([toBuffer(wbout)], {
type: 'application/octet-stream'
}); // 保存导出
FileSaver.saveAs(blob, "".concat(filename, ".").concat(type));
}
function handleExportEvent(params) {
switch (params.options.type) {
case 'xlsx':
toXLSX(params);
return false;
}
}
/**
* 基于 vxe-table 表格的增强插件,支持导出 xlsx 等格式
*/
var VXETablePluginExport = {
install: function install(xtable) {
Object.assign(xtable.types, {
xlsx: 1
});
xtable.interceptor.add('event.export', handleExportEvent);
}
};
_exports.VXETablePluginExport = VXETablePluginExport;
if (typeof window !== 'undefined' && window.VXETable) {
window.VXETable.use(VXETablePluginExport);
}
var _default = VXETablePluginExport;
_exports["default"] = _default;
});

1
dist/index.min.js vendored Normal file
View File

@@ -0,0 +1 @@
!function(e,t){if("function"==typeof define&&define.amd)define("vxe-table-plugin-export",["exports","xe-utils","xlsx","file-saver"],t);else if("undefined"!=typeof exports)t(exports,require("xe-utils"),require("xlsx"),require("file-saver"));else{var r={exports:{}};t(r.exports,e.XEUtils,e.xlsx,e.fileSaver),e.VXETablePluginExport=r.exports.default}}(this,function(e,x,v,y){"use strict";var t;function a(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return a=function(){return e},e}function r(e){if(e&&e.__esModule)return e;var t=a();if(t&&t.has(e))return t.get(e);var r={};if(null!=e){var n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if(Object.prototype.hasOwnProperty.call(e,o)){var i=n?Object.getOwnPropertyDescriptor(e,o):null;i&&(i.get||i.set)?Object.defineProperty(r,o,i):r[o]=e[o]}}return r.default=e,t&&t.set(e,r),r}function n(e){var t=e.options,n=e.columns,r=e.datas,o=t.filename,i=t.type,a=t.isHeader,l=t.original,u={};a&&n.forEach(function(e){u[e.id]=e.getTitle()});var s=r.map(function(t){var r={};return n.forEach(function(e){r[e.id]=l?x.default.get(t,e.property):t[e.id]}),r}),f=v.utils.book_new(),p=v.utils.json_to_sheet((a?[u]:[]).concat(s),{skipHeader:!0});v.utils.book_append_sheet(f,p,o);var c=v.write(f,{bookType:i,bookSST:!1,type:"binary"}),d=new Blob([function(e){for(var t=new ArrayBuffer(e.length),r=new Uint8Array(t),n=0;n!==e.length;++n)r[n]=255&e.charCodeAt(n);return t}(c)],{type:"application/octet-stream"});y.saveAs(d,"".concat(o,".").concat(i))}function o(e){switch(e.options.type){case"xlsx":return n(e),!1}}Object.defineProperty(e,"__esModule",{value:!0}),e.default=e.VXETablePluginExport=void 0,x=(t=x)&&t.__esModule?t:{default:t},v=r(v),y=r(y);var i={install:function(e){Object.assign(e.types,{xlsx:1}),e.interceptor.add("event.export",o)}};e.VXETablePluginExport=i,"undefined"!=typeof window&&window.VXETable&&window.VXETable.use(i);var l=i;e.default=l});

74
gulpfile.js Normal file
View File

@@ -0,0 +1,74 @@
const gulp = require('gulp')
const del = require('del')
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')
const rename = require('gulp-rename')
const replace = require('gulp-replace')
const sourcemaps = require('gulp-sourcemaps')
const ts = require('gulp-typescript')
const pack = require('./package.json')
const exportModuleName = 'VXETablePluginExport'
gulp.task('build_commonjs', function () {
return gulp.src(['depend.ts', 'index.ts'])
.pipe(sourcemaps.init())
.pipe(ts({
strict: true,
moduleResolution: 'node',
noImplicitAny: true,
target: 'es6',
lib: ['dom', 'es6']
}))
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(rename({
basename: 'index',
extname: '.common.js'
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'))
})
gulp.task('build_umd', function () {
return gulp.src(['depend.ts', 'index.ts'])
.pipe(ts({
strict: true,
moduleResolution: 'node',
noImplicitAny: true,
target: 'es6',
lib: ['dom', 'es6']
}))
.pipe(replace(`import XEUtils from 'xe-utils/methods/xe-utils';`, `import XEUtils from 'xe-utils';`))
.pipe(babel({
moduleId: pack.name,
presets: [
'@babel/env'
],
plugins: [
['@babel/transform-modules-umd', {
globals: {
[pack.name]: exportModuleName,
'xe-utils': 'XEUtils'
},
exactGlobals: true
}]
]
}))
.pipe(replace(`global.${exportModuleName} = mod.exports;`, `global.${exportModuleName} = mod.exports.default;`))
.pipe(gulp.dest('dist'))
.pipe(uglify())
.pipe(rename({
extname: '.min.js'
}))
.pipe(gulp.dest('dist'))
})
gulp.task('clear', () => {
return del([
'dist/depend.*'
])
})
gulp.task('build', gulp.series(gulp.parallel('build_commonjs', 'build_umd'), 'clear'))

61
index.ts Normal file
View File

@@ -0,0 +1,61 @@
import XEUtils from 'xe-utils/methods/xe-utils'
import VXETable from 'vxe-table/lib/vxe-table'
import * as XLSX from 'xlsx'
import * as FileSaver from 'file-saver'
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 toXLSX(params: any) {
const { options, columns, datas } = params
const { filename, type, isHeader, original } = options
const colHead: any = {}
if (isHeader) {
columns.forEach((column: any) => {
colHead[column.id] = column.getTitle()
})
}
const rowList = datas.map((row: any) => {
const item: any = {}
columns.forEach((column: any) => {
item[column.id] = original ? XEUtils.get(row, column.property) : row[column.id]
})
return item
})
const book = XLSX.utils.book_new()
const sheet = XLSX.utils.json_to_sheet((isHeader ? [colHead] : []).concat(rowList), { skipHeader: true })
// 转换数据
XLSX.utils.book_append_sheet(book, sheet, filename)
const wbout = XLSX.write(book, { bookType: type, bookSST: false, type: 'binary' })
const blob = new Blob([toBuffer(wbout)], { type: 'application/octet-stream' })
// 保存导出
FileSaver.saveAs(blob, `${filename}.${type}`)
}
function handleExportEvent(params: any) {
switch (params.options.type) {
case 'xlsx':
toXLSX(params)
return false
}
}
/**
* 基于 vxe-table 表格的增强插件,支持导出 xlsx 等格式
*/
export const VXETablePluginExport = {
install(xtable: typeof VXETable) {
Object.assign(xtable.types, { xlsx: 1 })
xtable.interceptor.add('event.export', handleExportEvent)
}
}
if (typeof window !== 'undefined' && window.VXETable) {
window.VXETable.use(VXETablePluginExport)
}
export default VXETablePluginExport

77
package.json Normal file
View File

@@ -0,0 +1,77 @@
{
"name": "vxe-table-plugin-export",
"version": "1.0.0",
"description": "基于 vxe-table 表格的增强插件,支持导出 xlsx 等格式",
"scripts": {
"lib": "gulp build"
},
"files": [
"dist",
"*.ts",
"*.d.ts"
],
"main": "dist/index.common.js",
"style": "dist/style.min.css",
"unpkg": "dist/index.min.js",
"jsdelivr": "dist/index.min.js",
"typings": "index.ts",
"devDependencies": {
"@babel/core": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/runtime": "^7.4.4",
"@types/file-saver": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^2.3.1",
"@typescript-eslint/parser": "^2.3.1",
"del": "^5.1.0",
"eslint": "^5.15.1",
"eslint-config-prettier": "^6.3.0",
"eslint-config-standard": "^12.0.0",
"eslint-friendly-formatter": "^4.0.1",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-typescript": "^0.14.0",
"file-saver": "^2.0.2",
"gulp": "^4.0.2",
"gulp-autoprefixer": "^6.1.0",
"gulp-babel": "^8.0.0",
"gulp-clean-css": "^4.2.0",
"gulp-concat": "^2.6.1",
"gulp-rename": "^1.4.0",
"gulp-replace": "^1.0.0",
"gulp-sass": "^4.0.2",
"gulp-sourcemaps": "^2.6.5",
"gulp-typescript": "^5.0.1",
"gulp-uglify": "^3.0.2",
"markdown-doctest": "^0.9.1",
"prettier": "^1.18.2",
"typescript": "^3.6.3",
"vue": "^2.6.10",
"vxe-table": "^2.6.12",
"xe-utils": "^2.2.11",
"xlsx": "^0.15.1"
},
"peerDependencies": {
"vxe-table": ">=2.6.12",
"xe-utils": ">=2.2.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/xuliangzhan/vxe-table-plugin-export.git"
},
"keywords": [
"vxe-table-plugin-export"
],
"author": {
"name": "Xu Liangzhan",
"email": "xu_liangzhan@163.com"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/xuliangzhan/vxe-table-plugin-export/issues"
},
"homepage": "https://github.com/xuliangzhan/vxe-table-plugin-export#readme"
}

13
tsconfig.json Normal file
View File

@@ -0,0 +1,13 @@
{
"files": [
"index.ts",
"depend.ts"
],
"compilerOptions": {
"strict": true,
"moduleResolution": "node",
"noImplicitAny": true,
"target": "es6",
"lib": ["dom", "es6"]
}
}