-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheetJS import.html
69 lines (61 loc) · 1.92 KB
/
sheetJS import.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
69
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>excel导入解析</title>
<script src="https://cdn.bootcss.com/xlsx/0.14.3/xlsx.full.min.js"></script>
</head>
<body>
<h2>excel导入解析</h2>
<input type="file" id="upload" />
<div id="demo"></div>
<script>
const flag = true; //是否将文件读取为二进制字符串
const upload = document.getElementById('upload');
const demo = document.getElementById('demo');
upload.onchange = event => {
const file = event.target.files[0];
if (!file) {
return;
}
const reader = new FileReader();
const name = file.name;
if (flag) {
reader.readAsBinaryString(file);
} else {
reader.readAsArrayBuffer(file);
}
reader.onload = e => {
const data = e.target.result;
let wb;
if (flag) {
wb = XLSX.read(data, {
type: 'binary'
});
} else {
const arr = fixdata(data);
wb = XLSX.read(btoa(arr), {
type: 'base64'
});
}
demo.innerHTML = JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
};
}
function fixdata(data) {
let o = "",
l = 0,
w = 10240;
for (; l < data.byteLength / w; ++l) {
o += String.fromCharCode.apply(
null,
new Uint8Array(
data.slice(l * w, l * w + w)
)
);
}
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
return o;
}
</script>
</body>
</html>