第一步
下载simple-excel.js和xlsx.core.min.js
可自行搜索也可按照下方地址下载
下载地址:
https://download.csdn.net/download/qq_34117170/10970330
第二步
读取
index.html
<html>
<head>
</head>
<body>
<input id="xlf" type="file" name="xlfile" />
<pre id="out"></pre>
<script src="jquery.js"></script>
<script src="simple-excel.js"></script>
<script src="xlsx.core.min.js"></script>
<script src="file.js"></script>
<script type="text/javascript">
var xlf = document.getElementById('xlf');
if(xlf.addEventListener) xlf.addEventListener('change', handleFile, false);
</script>
</body>
</html>
file.js
var X = XLSX;
function to_formulae(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var formulae = X.utils.get_formulae(workbook.Sheets[sheetName]);
if(formulae.length > 0){
result.push(formulae.join("\n"));
}
});
return result.join("\n");
}
function xw_xfer(data, cb) {
var worker = new Worker(rABS ? XW.rABS : XW.norABS);
worker.onmessage = function(e) {
switch(e.data.t) {
case 'ready': break;
case 'e': console.error(e.data.d); break;
default: xx=ab2str(e.data).replace(/\n/g,"\\n").replace(/\r/g,"\\r"); console.log("done"); cb(JSON.parse(xx)); break;
}
};
var val = s2ab(data);
worker.postMessage(val[1], [val[1]]);
}
function process_wb(wb) {
var output = to_formulae(wb).split("\n");
var arr=[];
var arrindex=[];
var isFirstRow = true;
var content=null;
var t=0;
for(var i=0; i<output.length;i++){
var temp = output[i];
var temp1=content;
content=output[i].split('=')[0].slice(-1);
if(!isFirstRow&&content!=temp1){
arr=[];
t=t+1
}
if(temp.indexOf(content)>-1) {
var value=output[i].split('=')[1]
arr.push(value);
arrindex[t]=arr;
isFirstRow=false;
}
}
// if(out.innerText === undefined) out.textContent = output;
// else out.innerText = output;
// if(typeof console !== 'undefined') console.log("output", new Date());
}
function handleFile(e) {
rABS = true;
use_worker = false;
var files = e.target.files;
var f = files[0];
var extension = f.name.split('.')[1];
if (extension === 'csv')
{
var file = f;
var csvParser = new SimpleExcel.Parser.CSV();
csvParser.setDelimiter(',');
csvParser.loadFile(file, function () {
// draw HTML table based on sheet data
var sheet = csvParser.getSheet();
var sheetlength = sheet.length || 0;
var title = [];
var Data = [];
var titleBT = [];
sheet.forEach(function (el, t) {
//数据自己组装
});
});
}
else{
var reader = new FileReader();
var name = f.name;
reader.onload = function(e) {
if(typeof console !== 'undefined') console.log("onload", new Date(), rABS, use_worker);
var data = e.target.result;
var wb = X.read(data, {type: 'binary'});
process_wb(wb);
};
reader.readAsBinaryString(f);
}