2013-04-13 13:56:25 +00:00
|
|
|
<!DOCTYPE html>
|
2014-01-28 16:38:02 +00:00
|
|
|
<!-- xlsx.js (C) 2013-2014 SheetJS http://sheetjs.com -->
|
2014-02-17 21:33:50 +00:00
|
|
|
<!-- vim: set ts=2: -->
|
2013-11-12 16:29:53 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>JS-XLSX Live Demo</title>
|
2013-02-18 03:40:27 +00:00
|
|
|
<style>
|
|
|
|
#drop{
|
|
|
|
border:2px dashed #bbb;
|
|
|
|
-moz-border-radius:5px;
|
|
|
|
-webkit-border-radius:5px;
|
|
|
|
border-radius:5px;
|
|
|
|
padding:25px;
|
|
|
|
text-align:center;
|
|
|
|
font:20pt bold,"Vollkorn";color:#bbb
|
|
|
|
}
|
2013-03-02 06:47:41 +00:00
|
|
|
#b64data{
|
|
|
|
width:100%;
|
|
|
|
}
|
2013-02-18 03:40:27 +00:00
|
|
|
</style>
|
2013-11-12 16:29:53 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
2014-01-28 16:38:02 +00:00
|
|
|
<b>JS-XLSX (XLSX/XLSB/XLSM) Live Demo</b><br />
|
2013-02-18 03:40:27 +00:00
|
|
|
<input type="radio" name="format" value="csv" checked> CSV<br>
|
|
|
|
<input type="radio" name="format" value="json"> JSON<br>
|
2013-05-06 14:54:42 +00:00
|
|
|
<input type="radio" name="format" value="form"> FORMULAE<br>
|
2013-02-18 03:40:27 +00:00
|
|
|
|
2014-01-28 16:38:02 +00:00
|
|
|
<div id="drop">Drop an XLSX or XLSM or XLSB file here to see sheet data.</div>
|
2013-03-02 06:47:41 +00:00
|
|
|
<textarea id="b64data">... or paste a base64-encoding here</textarea>
|
|
|
|
<input type="button" id="dotext" value="Click here to process the base64 text" onclick="b64it();"/>
|
2013-02-18 03:40:27 +00:00
|
|
|
<pre id="out"></pre>
|
|
|
|
<br />
|
|
|
|
<script src="jszip.js"></script>
|
|
|
|
<script src="xlsx.js"></script>
|
|
|
|
<script>
|
2014-01-23 06:20:19 +00:00
|
|
|
var rABS = typeof FileReader !== "undefined" && typeof FileReader.prototype !== "undefined" && typeof FileReader.prototype.readAsBinaryString !== "undefined";
|
2014-02-17 21:33:50 +00:00
|
|
|
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(o.length)));
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2013-11-22 22:58:11 +00:00
|
|
|
function xlsxworker(data, cb) {
|
|
|
|
var worker = new Worker('./xlsxworker.js');
|
|
|
|
worker.onmessage = function(e) {
|
|
|
|
switch(e.data.t) {
|
|
|
|
case 'ready': break;
|
2014-01-23 06:20:19 +00:00
|
|
|
case 'e': console.error(e.data.d); break;
|
2013-11-22 22:58:11 +00:00
|
|
|
case 'xlsx': cb(JSON.parse(e.data.d)); break;
|
|
|
|
}
|
|
|
|
};
|
2014-02-17 21:33:50 +00:00
|
|
|
var arr = rABS ? data : btoa(fixdata(data));
|
2014-01-23 06:20:19 +00:00
|
|
|
worker.postMessage({d:arr,b:rABS});
|
2013-11-22 22:58:11 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 03:40:27 +00:00
|
|
|
function get_radio_value( radioName ) {
|
2013-02-20 15:25:02 +00:00
|
|
|
var radios = document.getElementsByName( radioName );
|
|
|
|
for( var i = 0; i < radios.length; i++ ) {
|
|
|
|
if( radios[i].checked ) {
|
|
|
|
return radios[i].value;
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 03:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function to_json(workbook) {
|
2013-02-20 15:25:02 +00:00
|
|
|
var result = {};
|
|
|
|
workbook.SheetNames.forEach(function(sheetName) {
|
2013-05-03 14:23:19 +00:00
|
|
|
var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
|
|
|
|
if(roa.length > 0){
|
|
|
|
result[sheetName] = roa;
|
2013-02-20 15:25:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
2013-02-18 03:40:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 01:14:05 +00:00
|
|
|
function to_csv(workbook) {
|
|
|
|
var result = [];
|
|
|
|
workbook.SheetNames.forEach(function(sheetName) {
|
2013-05-03 14:23:19 +00:00
|
|
|
var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]);
|
|
|
|
if(csv.length > 0){
|
2013-03-15 01:14:05 +00:00
|
|
|
result.push("SHEET: " + sheetName);
|
|
|
|
result.push("");
|
2013-05-03 14:23:19 +00:00
|
|
|
result.push(csv);
|
2013-03-15 01:14:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return result.join("\n");
|
2013-02-18 03:40:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 14:54:42 +00:00
|
|
|
function to_formulae(workbook) {
|
|
|
|
var result = [];
|
|
|
|
workbook.SheetNames.forEach(function(sheetName) {
|
|
|
|
var formulae = XLSX.utils.get_formulae(workbook.Sheets[sheetName]);
|
|
|
|
if(formulae.length > 0){
|
|
|
|
result.push("SHEET: " + sheetName);
|
|
|
|
result.push("");
|
|
|
|
result.push(formulae.join("\n"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result.join("\n");
|
|
|
|
}
|
|
|
|
|
2013-03-02 06:47:41 +00:00
|
|
|
var tarea = document.getElementById('b64data');
|
|
|
|
function b64it() {
|
2013-05-03 14:23:19 +00:00
|
|
|
var wb = XLSX.read(tarea.value, {type: 'base64'});
|
|
|
|
process_wb(wb);
|
2013-03-02 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 14:23:19 +00:00
|
|
|
function process_wb(wb) {
|
2013-03-02 06:47:41 +00:00
|
|
|
var output = "";
|
2013-05-06 14:54:42 +00:00
|
|
|
switch(get_radio_value("format")) {
|
|
|
|
case "json":
|
2014-02-17 21:33:50 +00:00
|
|
|
output = JSON.stringify(to_json(wb), 2, 2);
|
2013-05-06 14:54:42 +00:00
|
|
|
break;
|
|
|
|
case "form":
|
|
|
|
output = to_formulae(wb);
|
2014-02-17 21:33:50 +00:00
|
|
|
break;
|
2013-05-06 14:54:42 +00:00
|
|
|
default:
|
2013-05-03 14:23:19 +00:00
|
|
|
output = to_csv(wb);
|
2013-03-02 06:47:41 +00:00
|
|
|
}
|
|
|
|
if(out.innerText === undefined) out.textContent = output;
|
|
|
|
else out.innerText = output;
|
|
|
|
}
|
|
|
|
|
2013-02-18 03:40:27 +00:00
|
|
|
var drop = document.getElementById('drop');
|
|
|
|
function handleDrop(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
var files = e.dataTransfer.files;
|
|
|
|
var i,f;
|
|
|
|
for (i = 0, f = files[i]; i != files.length; ++i) {
|
|
|
|
var reader = new FileReader();
|
|
|
|
var name = f.name;
|
|
|
|
reader.onload = function(e) {
|
|
|
|
var data = e.target.result;
|
2013-11-22 22:58:11 +00:00
|
|
|
if(typeof Worker !== 'undefined') {
|
|
|
|
xlsxworker(data, process_wb);
|
|
|
|
} else {
|
2014-01-23 06:20:19 +00:00
|
|
|
var wb;
|
|
|
|
if(rABS) {
|
|
|
|
wb = XLSX.read(data, {type: 'binary'});
|
|
|
|
} else {
|
2014-02-17 21:33:50 +00:00
|
|
|
var arr = fixdata(data);
|
2014-01-23 06:20:19 +00:00
|
|
|
wb = XLSX.read(btoa(arr), {type: 'base64'});
|
|
|
|
}
|
2013-11-22 22:58:11 +00:00
|
|
|
process_wb(wb);
|
|
|
|
}
|
2013-02-18 03:40:27 +00:00
|
|
|
};
|
2014-01-23 06:20:19 +00:00
|
|
|
if(rABS) reader.readAsBinaryString(f);
|
|
|
|
else reader.readAsArrayBuffer(f);
|
2013-02-18 03:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDragover(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
e.dataTransfer.dropEffect = 'copy';
|
|
|
|
}
|
2013-03-05 22:01:01 +00:00
|
|
|
|
2013-04-13 13:56:25 +00:00
|
|
|
if(drop.addEventListener) {
|
|
|
|
drop.addEventListener('dragenter', handleDragover, false);
|
2013-03-05 22:01:01 +00:00
|
|
|
drop.addEventListener('dragover', handleDragover, false);
|
|
|
|
drop.addEventListener('drop', handleDrop, false);
|
|
|
|
}
|
2013-02-18 03:40:27 +00:00
|
|
|
</script>
|
2013-11-12 16:29:53 +00:00
|
|
|
</body>
|
|
|
|
</html>
|