Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SheetJSDev
Created May 28, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SheetJSDev/80c3a7cd04cdefdfaa86 to your computer and use it in GitHub Desktop.
Save SheetJSDev/80c3a7cd04cdefdfaa86 to your computer and use it in GitHub Desktop.
/* generate CSV output for every sheet */
function to_csv(workbook) {
var result = [];
workbook.SheetNames.forEach(function(sheetName) {
var csv = XLSX.utils.make_csv(workbook.Sheets[sheetName]);
if(csv.length > 0){
result.push("SHEET: " + sheetName);
result.push("");
result.push(csv);
}
});
return result.join("\n");
}
/* Write to the page (`out` is a PRE element on the page) */
function process_wb(wb) {
var output = to_csv(wb);
if(out.innerText === undefined) out.textContent = output;
else out.innerText = output;
}
/* set up XMLHttpRequest */
var url = "test_files/formula_stress_test_ajax.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var wb = XLSX.read(bstr, {type:"binary"});
process_wb(wb);
}
oReq.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment