xsheetjs/index.html

249 lines
7.5 KiB
HTML
Raw Permalink Normal View History

<!DOCTYPE html>
<!-- xlsx.js (C) 2013-present SheetJS http://sheetjs.com -->
2014-02-17 21:33:50 +00:00
<!-- vim: set ts=2: -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SheetJS Live Demo</title>
2013-02-18 03:40:27 +00:00
<style>
#b64data{
width:100%;
}
2017-04-20 03:24:48 +00:00
a { text-decoration: none }
2022-04-16 01:19:21 +00:00
#drop-zone{
background: white;
position: fixed;
top: 0px;
left: 0px;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
z-index: -1;
}
#drop-zone p, #drop-zone svg { pointer-events: none }
#drop-zone svg { margin-right: 5px }
2013-02-18 03:40:27 +00:00
</style>
</head>
<body>
2017-04-20 03:24:48 +00:00
<pre>
<b><a href="http://sheetjs.com">SheetJS Data Preview Live Demo</a></b>
(Base64 text works back to IE6; drag and drop works back to IE10)
<a href="https://github.com/SheetJS/js-xlsx">Source Code Repo</a>
<a href="https://github.com/SheetJS/js-xlsx/issues">Issues? Something look weird? Click here and report an issue</a>
Output Format: <select name="format" onchange="setfmt()">
<option value="csv" selected> CSV</option>
<option value="json"> JSON</option>
<option value="form"> FORMULAE</option>
2017-04-20 03:24:48 +00:00
<option value="html"> HTML</option>
2022-04-16 01:19:21 +00:00
<option value="xlsx"> XLSX</option>
</select><br />
2022-04-16 01:19:21 +00:00
<input type="file" name="xlfile" id="xlf" />
<b> Drop a spreadsheet file anywhere in the page to see sheet data </b>
2017-04-20 03:24:48 +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();"/><br />
2017-04-20 03:24:48 +00:00
<b>Advanced Demo Options:</b>
2022-04-16 01:19:21 +00:00
Use Web Workers when available: <input type="checkbox" name="useworker" checked>
Always use UTF8 for CSV / text: <input type="checkbox" name="useutf8" checked>
2017-04-20 03:24:48 +00:00
</pre>
2013-02-18 03:40:27 +00:00
<pre id="out"></pre>
2017-04-20 03:24:48 +00:00
<div id="htmlout"></div>
2022-04-16 01:19:21 +00:00
<div id="drop-zone">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M17 13h-10v-1h10v1zm0-4h-10v1h10v-1zm0-3h-10v1h10v-1zm-15-1v-5h6v2h-4v3h-2zm8-5v2h4v-2h-4zm6 2h4v3h2v-5h-6v2zm6 5h-2v4h2v-4zm-20 10h2v-4h-2v4zm18-4v.543c0 4.107-6 2.457-6 2.457s1.518 6-2.638 6h-1.362v2h2.189c3.163 0 9.811-7.223 9.811-9.614v-1.386h-2zm-18-2h2v-4h-2v4zm2 11v-3h-2v5h6v-2h-4z"/></svg>
<p>Drop a spreadsheet file here to see sheet data</p>
</div>
2013-02-18 03:40:27 +00:00
<br />
2022-04-16 01:19:21 +00:00
<script src="dist/shim.min.js"></script>
<script src="dist/xlsx.full.min.js"></script>
2013-02-18 03:40:27 +00:00
<script>
/*jshint browser:true */
/* eslint-env browser */
2022-04-16 01:19:21 +00:00
/*global Uint8Array, Uint16Array, ArrayBuffer */
/*global XLSX */
/* eslint no-use-before-define:0 */
var global_wb;
var process_wb = (function() {
var OUT = document.getElementById('out');
var HTMLOUT = document.getElementById('htmlout');
2014-02-17 21:33:50 +00:00
var get_format = (function() {
var radios = document.getElementsByName( "format" );
return function() {
for(var i = 0; i < radios.length; ++i) if(radios[i].checked || radios.length === 1) return radios[i].value;
};
})();
var to_json = function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
2021-10-03 01:41:36 +00:00
var roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {header:1});
if(roa.length) result[sheetName] = roa;
});
return JSON.stringify(result, 2, 2);
2013-11-22 22:58:11 +00:00
};
var to_csv = function to_csv(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
2021-10-03 01:41:36 +00:00
var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]);
if(csv.length){
result.push("SHEET: " + sheetName);
result.push("");
result.push(csv);
}
});
return result.join("\n");
};
2013-02-18 03:40:27 +00:00
var to_fmla = function to_fmla(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
2021-10-03 01:41:36 +00:00
var formulae = XLSX.utils.get_formulae(workbook.Sheets[sheetName]);
if(formulae.length){
result.push("SHEET: " + sheetName);
result.push("");
result.push(formulae.join("\n"));
}
});
return result.join("\n");
};
2013-02-18 03:40:27 +00:00
var to_html = function to_html(workbook) {
HTMLOUT.innerHTML = "";
workbook.SheetNames.forEach(function(sheetName) {
2021-10-03 01:41:36 +00:00
var htmlstr = XLSX.write(workbook, {sheet:sheetName, type:'string', bookType:'html'});
HTMLOUT.innerHTML += htmlstr;
});
return "";
};
2013-02-18 03:40:27 +00:00
2022-04-16 01:19:21 +00:00
var to_xlsx = function to_xlsx(workbook) {
HTMLOUT.innerHTML = "";
XLSX.writeFile(workbook, "SheetJSTest.xlsx");
return "";
};
return function process_wb(wb) {
global_wb = wb;
var output = "";
switch(get_format()) {
case "form": output = to_fmla(wb); break;
case "html": output = to_html(wb); break;
case "json": output = to_json(wb); break;
2022-04-16 01:19:21 +00:00
case "xlsx": output = to_xlsx(wb); break;
default: output = to_csv(wb);
2013-05-06 14:54:42 +00:00
}
if(OUT.innerText === undefined) OUT.textContent = output;
else OUT.innerText = output;
if(typeof console !== 'undefined') console.log("output", new Date());
};
})();
2017-04-20 03:24:48 +00:00
var setfmt = window.setfmt = function setfmt() { if(global_wb) process_wb(global_wb); };
var b64it = window.b64it = (function() {
var tarea = document.getElementById('b64data');
return function b64it() {
if(typeof console !== 'undefined') console.log("onload", new Date());
2021-10-03 01:41:36 +00:00
var wb = XLSX.read(tarea.value, {type:'base64', WTF:false});
process_wb(wb);
};
})();
var do_file = (function() {
var use_worker = typeof Worker !== 'undefined';
var domwork = document.getElementsByName("useworker")[0];
if(!use_worker) domwork.disabled = !(domwork.checked = false);
2022-04-16 01:19:21 +00:00
var use_utf8 = false;
var xw = function xw(data, cb) {
2021-10-03 01:41:36 +00:00
var worker = new Worker('./xlsxworker.js');
worker.onmessage = function(e) {
switch(e.data.t) {
case 'ready': break;
case 'e': console.error(e.data.d); break;
2021-10-03 01:41:36 +00:00
case 'xlsx': cb(JSON.parse(e.data.d)); break;
}
};
2022-04-16 01:19:21 +00:00
worker.postMessage({d:data,b:'array',c:use_utf8 ? 65001 : void 0});
};
return function do_file(files) {
use_worker = domwork.checked;
2022-04-16 01:19:21 +00:00
use_utf8 = document.getElementsByName("useutf8")[0].checked;
var f = files[0];
2013-02-18 03:40:27 +00:00
var reader = new FileReader();
reader.onload = function(e) {
2021-10-03 01:41:36 +00:00
if(typeof console !== 'undefined') console.log("onload", new Date(), use_worker);
var data = new Uint8Array(e.target.result);
if(use_worker) xw(data, process_wb);
2022-04-16 01:19:21 +00:00
else process_wb(XLSX.read(data, {type: 'array', codepage: use_utf8 ? 65001 : void 0}));
2013-02-18 03:40:27 +00:00
};
2021-10-03 01:41:36 +00:00
reader.readAsArrayBuffer(f);
};
})();
(function() {
2022-04-20 17:31:11 +00:00
var dropZone = document.getElementById('drop-zone');
2022-04-16 01:19:21 +00:00
if(!dropZone.addEventListener && !window.addEventListener) return;
function handleDrop(e) {
2022-04-16 01:19:21 +00:00
dropZoneDisplay(e, false);
do_file(e.dataTransfer.files);
2013-02-18 03:40:27 +00:00
}
function handleDragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
2022-04-16 01:19:21 +00:00
function dropZoneDisplay(e, show){
e.stopPropagation();
e.preventDefault();
var opacity = show ? '1' : '0';
var zIndex = show ? '1' : '-1';
dropZone.style.opacity = opacity;
dropZone.style.zIndex = zIndex;
}
2022-04-20 17:31:11 +00:00
window.addEventListener('drop' , handleDrop);
window.addEventListener('dragover' , handleDragover);
2022-04-16 01:19:21 +00:00
window.addEventListener('dragenter' , function(e){
dropZoneDisplay(e, true);
2022-04-20 17:31:11 +00:00
});
2022-04-16 01:19:21 +00:00
dropZone.addEventListener('dragleave' , function(e){
dropZoneDisplay(e, false);
2022-04-20 17:31:11 +00:00
});
})();
(function() {
var xlf = document.getElementById('xlf');
if(!xlf.addEventListener) return;
function handleFile(e) { do_file(e.target.files); }
xlf.addEventListener('change', handleFile, false);
})();
2017-04-20 03:24:48 +00:00
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36810333-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>