Skip to content

Instantly share code, notes, and snippets.

@intelguasoft
Created May 28, 2019 22:06
Show Gist options
  • Save intelguasoft/1fc1e740b353b2936a047c122c878ec8 to your computer and use it in GitHub Desktop.
Save intelguasoft/1fc1e740b353b2936a047c122c878ec8 to your computer and use it in GitHub Desktop.
Metodo para procesar la data de una determinada hoja de un archivo de Excel y devolverla en formato JSON.
/**
* Metodo para procesar la data de una determinada hoja de un archivo de Excel y devolverla en formato JSON.
* Se utilizo la librería SheetJS para este ejemplo.
* Henry Díaz, <hnrdiaz@gmail.com>
**/
$('#btn-importar').on('click', function(e) {
e.preventDefault();
$('#movimiento').show();
let hoja = e.target.value;
let datos;
// console.log($('#input-excel')[0].files);
// $('#datos')[0].innerHTML.empty();
let reader = new FileReader();
reader.readAsArrayBuffer($('#input-excel')[0].files[0]);
reader.onload = function(e) {
var data = new Uint8Array(reader.result);
var wb = XLSX.read(data, {
type: 'array'
});
var worksheet = wb.Sheets[hoja];
datos = XLSX.utils.sheet_to_json(worksheet, {
header: 1,
raw: true
});
datos.forEach(function(element) {
$.post('/alumnos', element, function() {
alert("Importando datos de alumnos.");
$('#movimiento').hide();
})
.fail(function(e) {
alert("error");
$('#movimiento').hide();
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment