Skip to content

Commit 8f92170

Browse files
committed
init
1 parent cb8c27f commit 8f92170

File tree

14 files changed

+765
-0
lines changed

14 files changed

+765
-0
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [
3+
["env", { "modules": false }],
4+
"stage-3"
5+
]
6+
}

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.DS_Store
2+
node_modules/
3+
dist/
4+
npm-debug.log
5+
yarn-error.log
6+
7+
# Editor directories and files
8+
.idea
9+
*.suo
10+
*.ntvs*
11+
*.njsproj
12+
*.sln

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 D2 Projects
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# vue-table-export

index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>vue-table-export</title>
6+
</head>
7+
<body>
8+
<div id="app"></div>
9+
<script src="/dist/vue-table-export.js"></script>
10+
</body>
11+
</html>

src/App.vue

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<template>
2+
<div id="app">
3+
<button @click="exportCsv">exportCsv</button>
4+
<button @click="exportExcel">exportExcel</button>
5+
<button @click="exportTxt">exportTxt</button>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: 'app',
12+
data () {
13+
return {
14+
columns: [
15+
{ label: 'ID', prop: 'id' },
16+
{ label: '名称', prop: 'name' },
17+
{ label: '创建日期', prop: 'creatDate' },
18+
{ label: '地址', prop: 'address' },
19+
{ label: '邮编', prop: 'zip' }
20+
],
21+
data: [
22+
{ is: 1, name: 'FairyEver', creatDate: '2018-12-16', address: '北京市', zip: '100000' }
23+
]
24+
}
25+
},
26+
methods: {
27+
exportCsv (params = {}) {
28+
this.$export.csv({
29+
columns: this.columns,
30+
data: this.data
31+
})
32+
.then(() => {
33+
this.$message('导出CSV成功')
34+
})
35+
},
36+
exportExcel () {
37+
this.$export.excel({
38+
columns: this.columns,
39+
data: this.data,
40+
header: '导出 Excel',
41+
merges: ['A1', 'E1']
42+
})
43+
.then(() => {
44+
this.$message('导出表格成功')
45+
})
46+
},
47+
exportTxt () {
48+
this.$export.txt({
49+
text: 'Hello World',
50+
title: '文本'
51+
})
52+
.then(() => {
53+
this.$message('导出文本成功')
54+
})
55+
}
56+
}
57+
}
58+
</script>

src/lib/_blob.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/* eslint-disable */
2+
/* Blob.js
3+
* A Blob implementation.
4+
* 2014-05-27
5+
*
6+
* By Eli Grey, http://eligrey.com
7+
* By Devin Samarin, https://github.com/eboyjr
8+
* License: X11/MIT
9+
* See LICENSE.md
10+
*/
11+
12+
/*global self, unescape */
13+
/*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
14+
plusplus: true */
15+
16+
/*! @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */
17+
18+
(function (view) {
19+
"use strict";
20+
21+
view.URL = view.URL || view.webkitURL;
22+
23+
if (view.Blob && view.URL) {
24+
try {
25+
new Blob;
26+
return;
27+
} catch (e) {}
28+
}
29+
30+
// Internally we use a BlobBuilder implementation to base Blob off of
31+
// in order to support older browsers that only have BlobBuilder
32+
var BlobBuilder = view.BlobBuilder || view.WebKitBlobBuilder || view.MozBlobBuilder || (function(view) {
33+
var
34+
get_class = function(object) {
35+
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
36+
}
37+
, FakeBlobBuilder = function BlobBuilder() {
38+
this.data = [];
39+
}
40+
, FakeBlob = function Blob(data, type, encoding) {
41+
this.data = data;
42+
this.size = data.length;
43+
this.type = type;
44+
this.encoding = encoding;
45+
}
46+
, FBB_proto = FakeBlobBuilder.prototype
47+
, FB_proto = FakeBlob.prototype
48+
, FileReaderSync = view.FileReaderSync
49+
, FileException = function(type) {
50+
this.code = this[this.name = type];
51+
}
52+
, file_ex_codes = (
53+
"NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR "
54+
+ "NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR"
55+
).split(" ")
56+
, file_ex_code = file_ex_codes.length
57+
, real_URL = view.URL || view.webkitURL || view
58+
, real_create_object_URL = real_URL.createObjectURL
59+
, real_revoke_object_URL = real_URL.revokeObjectURL
60+
, URL = real_URL
61+
, btoa = view.btoa
62+
, atob = view.atob
63+
64+
, ArrayBuffer = view.ArrayBuffer
65+
, Uint8Array = view.Uint8Array
66+
;
67+
FakeBlob.fake = FB_proto.fake = true;
68+
while (file_ex_code--) {
69+
FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
70+
}
71+
if (!real_URL.createObjectURL) {
72+
URL = view.URL = {};
73+
}
74+
URL.createObjectURL = function(blob) {
75+
var
76+
type = blob.type
77+
, data_URI_header
78+
;
79+
if (type === null) {
80+
type = "application/octet-stream";
81+
}
82+
if (blob instanceof FakeBlob) {
83+
data_URI_header = "data:" + type;
84+
if (blob.encoding === "base64") {
85+
return data_URI_header + ";base64," + blob.data;
86+
} else if (blob.encoding === "URI") {
87+
return data_URI_header + "," + decodeURIComponent(blob.data);
88+
} if (btoa) {
89+
return data_URI_header + ";base64," + btoa(blob.data);
90+
} else {
91+
return data_URI_header + "," + encodeURIComponent(blob.data);
92+
}
93+
} else if (real_create_object_URL) {
94+
return real_create_object_URL.call(real_URL, blob);
95+
}
96+
};
97+
URL.revokeObjectURL = function(object_URL) {
98+
if (object_URL.substring(0, 5) !== "data:" && real_revoke_object_URL) {
99+
real_revoke_object_URL.call(real_URL, object_URL);
100+
}
101+
};
102+
FBB_proto.append = function(data/*, endings*/) {
103+
var bb = this.data;
104+
// decode data to a binary string
105+
if (Uint8Array && (data instanceof ArrayBuffer || data instanceof Uint8Array)) {
106+
var
107+
str = ""
108+
, buf = new Uint8Array(data)
109+
, i = 0
110+
, buf_len = buf.length
111+
;
112+
for (; i < buf_len; i++) {
113+
str += String.fromCharCode(buf[i]);
114+
}
115+
bb.push(str);
116+
} else if (get_class(data) === "Blob" || get_class(data) === "File") {
117+
if (FileReaderSync) {
118+
var fr = new FileReaderSync;
119+
bb.push(fr.readAsBinaryString(data));
120+
} else {
121+
// async FileReader won't work as BlobBuilder is sync
122+
throw new FileException("NOT_READABLE_ERR");
123+
}
124+
} else if (data instanceof FakeBlob) {
125+
if (data.encoding === "base64" && atob) {
126+
bb.push(atob(data.data));
127+
} else if (data.encoding === "URI") {
128+
bb.push(decodeURIComponent(data.data));
129+
} else if (data.encoding === "raw") {
130+
bb.push(data.data);
131+
}
132+
} else {
133+
if (typeof data !== "string") {
134+
data += ""; // convert unsupported types to strings
135+
}
136+
// decode UTF-16 to binary string
137+
bb.push(unescape(encodeURIComponent(data)));
138+
}
139+
};
140+
FBB_proto.getBlob = function(type) {
141+
if (!arguments.length) {
142+
type = null;
143+
}
144+
return new FakeBlob(this.data.join(""), type, "raw");
145+
};
146+
FBB_proto.toString = function() {
147+
return "[object BlobBuilder]";
148+
};
149+
FB_proto.slice = function(start, end, type) {
150+
var args = arguments.length;
151+
if (args < 3) {
152+
type = null;
153+
}
154+
return new FakeBlob(
155+
this.data.slice(start, args > 1 ? end : this.data.length)
156+
, type
157+
, this.encoding
158+
);
159+
};
160+
FB_proto.toString = function() {
161+
return "[object Blob]";
162+
};
163+
FB_proto.close = function() {
164+
this.size = this.data.length = 0;
165+
};
166+
return FakeBlobBuilder;
167+
}(view));
168+
169+
view.Blob = function Blob(blobParts, options) {
170+
var type = options ? (options.type || "") : "";
171+
var builder = new BlobBuilder();
172+
if (blobParts) {
173+
for (var i = 0, len = blobParts.length; i < len; i++) {
174+
builder.append(blobParts[i]);
175+
}
176+
}
177+
return builder.getBlob(type);
178+
};
179+
}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));

src/lib/_csv.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
inspired by https://www.npmjs.com/package/react-csv-downloader
3+
now removed from Github
4+
inspired by https://github.com/iview/iview
5+
*/
6+
7+
/* eslint-disable */
8+
9+
const newLine = '\r\n';
10+
const appendLine = (content, row, { separator, quoted }) => {
11+
const line = row.map(data => {
12+
if (!quoted) return data;
13+
// quote data
14+
data = typeof data === 'string' ? data.replace(/"/g, '"') : data;
15+
return `"${data}"`;
16+
});
17+
content.push(line.join(separator));
18+
};
19+
20+
const defaults = {
21+
separator: ',',
22+
quoted: false
23+
};
24+
25+
export default function csv(columns, datas, options, noHeader = false) {
26+
options = Object.assign({}, defaults, options);
27+
let columnOrder;
28+
const content = [];
29+
const column = [];
30+
31+
if (columns) {
32+
columnOrder = columns.map(v => {
33+
if (typeof v === 'string') return v;
34+
if (!noHeader) {
35+
column.push(typeof v.label !== 'undefined' ? v.label : v.prop);
36+
}
37+
return v.prop;
38+
});
39+
if (column.length > 0) appendLine(content, column, options);
40+
} else {
41+
columnOrder = [];
42+
datas.forEach(v => {
43+
if (!Array.isArray(v)) {
44+
columnOrder = columnOrder.concat(Object.keys(v));
45+
}
46+
});
47+
if (columnOrder.length > 0) {
48+
columnOrder = columnOrder.filter((value, index, self) => self.indexOf(value) === index);
49+
if (!noHeader) appendLine(content, columnOrder, options);
50+
}
51+
}
52+
53+
if (Array.isArray(datas)) {
54+
datas.forEach(row => {
55+
if (!Array.isArray(row)) {
56+
row = columnOrder.map(k => (typeof row[k] !== 'undefined' ? row[k] : ''));
57+
}
58+
appendLine(content, row, options);
59+
});
60+
}
61+
return content.join(newLine);
62+
}

0 commit comments

Comments
 (0)