Skip to content

Commit

Permalink
add: excel demo
Browse files Browse the repository at this point in the history
  • Loading branch information
xrkffgg committed Apr 3, 2020
1 parent d5eb2b9 commit 28a79e4
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Kvue",
"version": "0.0.3",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
1 change: 1 addition & 0 deletions src/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
id="his"
:title="his_title"
:show-close="false"
top="6vh"
center
:visible.sync="dialoghis">
<center>
Expand Down
56 changes: 43 additions & 13 deletions src/components/jsCom/003.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
<div class="main-title">Excel Demo</div>
<div class="main-title-time num">2020-04-03</div>
<div class="main-title-tip">
应好多小伙伴的要求,先增加一个小 Demo。该 Demo 仅用于展示功能,若有 Bug,请谅解,也可在 https://github.com/xrkffgg/Kvue/issues 提出来。目录 src/assets/excel 有一个待上传的 excel 例子
应好多小伙伴的要求,先增加一个小 Demo。该 Demo 仅用于展示功能,略粗糙,若有 Bug,请谅解,也可在 https://github.com/xrkffgg/Kvue/issues 提出来。可点击下载 Demo Excel,若因网络问题,可在目录 src/assets/excel 中查看。导出的 Excel 会自动下载到浏览器默认的下载地址中
</div>
<div class="main-content">
<el-tag>导入</el-tag>
<el-upload
action=""
style="margin-top:-32px; margin-left: 100px;"
:on-change="handleChange"
:file-list="fileListUpload"
limit="1"
accept=".xls,.xlsx"
:auto-upload="false">
<el-button :loading="disbtn" size="small" type="primary" icon="el-icon-upload2" >点击上传</el-button>
<div slot="tip" class="el-upload__tip">只 能 上 传 xlsx / xls 文 件</div>
</el-upload>
<div style="display: flex; width: 340px; justify-content: space-between;">
<el-button type="primary" plain size="small" @click="doDown">下载 Excel</el-button>
<el-upload
action=""
:on-change="handleChange"
:file-list="fileListUpload"
:show-file-list="false"
accept=".xls,.xlsx"
:auto-upload="false">
<el-button :loading="disbtn" size="small" type="primary">上传 Excel</el-button>
</el-upload>
<el-button type="success" plain :loading="disbtn" size="small" @click="doOut">导出 Excel</el-button>
</div>
<el-table
:data="list"
stripe
Expand Down Expand Up @@ -51,6 +52,11 @@ export default {
},
methods: {
doDown(){
let url = 'https://raw.githubusercontent.com/xrkffgg/Kvue/master/src/assets/excel/demo.xlsx'
window.location.href = url
},
// 上传附件
handleChange (file, fileList) {
this.fileTemp = file.raw
Expand Down Expand Up @@ -121,6 +127,30 @@ export default {
reader.readAsBinaryString(f)
}
},
// out
doOut(){
this.disbtn = true
this.getExcel(this.list)
},
// 数组导入Excel
getExcel(arr) {       
require.ensure([], () => {
// 这里 require 的地址指向你项目中 Export2Excel.js 文件存放地址
const { export_json_to_excel } = require('../../js/Export2Excel.js')
const tHeader = ['名字', '年龄']
const filterVal = ['name', 'age']
const list = arr
const data = this.formatJson(filterVal, list)
export_json_to_excel(tHeader, data, '奥力给')
this.disbtn = false
})
},
formatJson(filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
},
},
}
</script>
10 changes: 10 additions & 0 deletions src/components/notes/hisData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const HIS = [
// fix opt new 修复/优化/新增
{
time : '2020-04-03',
version : 'v0.1.0',
con : [
{
type : 'new',
text : `新增 上传导出 Excel Demo`
},
],
},
{
time : '2019-08-15',
version : 'v0.0.3',
Expand Down
179 changes: 179 additions & 0 deletions src/js/Blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/* 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));
Loading

0 comments on commit 28a79e4

Please sign in to comment.