xsheetjs/index.html

97 lines
2.4 KiB
HTML

<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
}
#b64data{
width:100%;
}
</style>
<b>JS-XLSX Live Demo</b><br />
<input type="radio" name="format" value="csv" checked> CSV<br>
<input type="radio" name="format" value="json"> JSON<br>
<div id="drop">Drop an XLSX file here to see data in CSV or JSON.</div>
<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();"/>
<pre id="out"></pre>
<br />
<script src="jszip.js"></script>
<script src="xlsx.js"></script>
<script>
function get_radio_value( radioName ) {
var radios = document.getElementsByName( radioName );
for( var i = 0; i < radios.length; i++ ) {
if( radios[i].checked ) {
return radios[i].value;
}
}
}
//Row object array form:
//Each row is an object with column headers as keys
function to_json(workbook) {
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
var rObjArr = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if(rObjArr.length > 0){
result[sheetName] = rObjArr;
}
});
return result;
}
function to_csv(sheet) {
return XLSX.utils.sheet_to_csv(sheet);
}
var tarea = document.getElementById('b64data');
function b64it() {
var xlsx = XLSX.read(tarea.value, {type: 'base64'});
process_xlsx(xlsx);
}
function process_xlsx(xlsx) {
var output = "";
if(get_radio_value("format") === "json"){
output = JSON.stringify(to_json(xlsx), 2, 2);
} else {
output = to_csv(xlsx.Sheets[xlsx.SheetNames[0]]);
}
if(out.innerText === undefined) out.textContent = output;
else out.innerText = output;
}
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;
var xlsx = XLSX.read(data, {type: 'binary'});
process_xlsx(xlsx);
};
reader.readAsBinaryString(f);
}
}
function handleDragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', handleDrop, false);
</script>