forked from stephen-hardy/xlsx.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
68 lines (62 loc) · 1.79 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<title>Example of xlsx.js</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="build/build.js"></script>
<style>
body {
padding:100px;
}
</style>
</head>
<body>
<div class="col">
</div>
<button id="save">save</button>
<script>
var xlsx = require('xlsx.js');
var $ = require('jquery');
var data = [
['Person', 'Dogs'],
['a', 0],
['b', 1],
];
$('#save').on('click', function() {
var file = {
worksheets: [[]], // worksheets has one empty worksheet (array)
creator: 'John Smith', created: new Date('8/16/2012'),
lastModifiedBy: 'Larry Jones', modified: new Date(),
activeWorksheet: 0
}, w = file.worksheets[0]; // cache current worksheet
w.name = '测试';
w.data = data;
window.location = xlsx(file).href();
});
//test read xlsx file
var xhr = new XMLHttpRequest();
xhr.open('GET', 'download.xlsx', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (this.status == 200) {
var uInt8Array = new Uint8Array(this.response);
var i = uInt8Array.length;
var binaryString = new Array(i);
while (i--)
{
binaryString[i] = String.fromCharCode(uInt8Array[i]);
}
var data = binaryString.join('');
var base64 = window.btoa(data);
console.log(xlsx(base64).worksheets[0]);
//[
//[[{value: 'Person'}], [{value: 'Dogs'}]],
//[[{value: 'a'}], [{value: '0'}]],
//[[{value: 'b'}], [{value: '1'}]]
//]
}
};
xhr.send();
</script>
</body>
</html>