<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>
</head>
<body>
<input type="file" onchange="importf(this)" />
<div id="demo"></div>
<script>
var rABS = false; //是否将文件读取为二进制字符串
function importf(obj) {//导入
if (!obj.files) { return; }
var f = obj.files[0];
{
var reader = new FileReader();
var name = f.name;
reader.onload = function (e) {
var data = e.target.result;
var wb;
if (rABS) {
wb = XLSX.read(data, { type: 'binary' });
} else {
var arr = fixdata(data);
wb = XLSX.read(btoa(arr), { type: 'base64' });
}
document.getElementById("demo").innerHTML = JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
};
if (rABS) reader.readAsBinaryString(f);
else reader.readAsArrayBuffer(f);
}
}
function fixdata(data) {
var 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>
var rABS = false; //是否将文件读取为二进制字符串
var tdata;
function importf(obj) {
if (!obj.files) { return; }
var f = obj.files[0];
{
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
var wb;
if (rABS) {
wb = XLSX.read(data, { type: 'binary' }); // 文件以二进制格式处理
} else {
var arr = fixdata(data);
wb = XLSX.read(btoa(arr), { type: 'base64' }); // 文件以base64格式处理
}
// 将数据转化为josn格式
tdata= JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
tdata=JSON.parse(tdata); // 将获取到的excel转化为对象
// 后面的操作可根据项目情况来决定,一般来说会先传到数据库,之后再显示在页面。
}
}
}