Created
June 13, 2017 12:27
-
-
Save sweet-zone/e592e6332d3b792a3f50f0dc05a1ca5f to your computer and use it in GitHub Desktop.
xlxs demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <!-- xlsx.js (C) 2013-present SheetJS http://sheetjs.com --> | |
| <!-- vim: set ts=2: --> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| <title>JS-XLSX Demo</title> | |
| </head> | |
| <body> | |
| <pre> | |
| <input type="file" name="xlfile" id="xlf" /> click here to select a file | |
| <pre id="out"></pre> | |
| <script src="xlsx.full.min.js"></script> | |
| <script> | |
| /*jshint browser:true */ | |
| /*global XLSX */ | |
| var X = XLSX; | |
| var XW = { | |
| /* worker message */ | |
| msg: 'xlsx', | |
| /* worker scripts */ | |
| rABS: './xlsxworker2.js', | |
| norABS: './xlsxworker1.js', | |
| noxfer: './xlsxworker.js' | |
| }; | |
| var rABS = typeof FileReader !== "undefined" && typeof FileReader.prototype !== "undefined" && typeof FileReader.prototype.readAsBinaryString !== "undefined"; | |
| var use_worker = typeof Worker !== 'undefined'; | |
| var transferable = true | |
| function to_json(workbook) { | |
| var result = {}; | |
| workbook.SheetNames.forEach(function(sheetName) { | |
| var roa = X.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]); | |
| if(roa.length > 0){ | |
| result[sheetName] = roa; | |
| } | |
| }); | |
| return result; | |
| } | |
| function process_wb(wb) { | |
| global_wb = wb; | |
| var output = ""; | |
| output = JSON.stringify(to_json(wb), null, 2); | |
| out.textContent = output; | |
| } | |
| var xlf = document.getElementById('xlf'); | |
| function handleFile(e) { | |
| rABS = true | |
| use_worker = true | |
| var files = e.target.files; | |
| var f = files[0]; | |
| { | |
| var reader = new FileReader(); | |
| var name = f.name; | |
| reader.onload = function(e) { | |
| var data = e.target.result; | |
| var wb; | |
| wb = X.read(data, {type: 'binary'}); | |
| process_wb(wb); | |
| }; | |
| if(rABS) reader.readAsBinaryString(f); | |
| else reader.readAsArrayBuffer(f); | |
| } | |
| } | |
| if(xlf.addEventListener) xlf.addEventListener('change', handleFile, false); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment