Skip to content

Instantly share code, notes, and snippets.

@brunovcosta
Last active May 17, 2021 23:44
Show Gist options
  • Save brunovcosta/e6626b9a8afa8e13cadca8f86d98ca38 to your computer and use it in GitHub Desktop.
Save brunovcosta/e6626b9a8afa8e13cadca8f86d98ca38 to your computer and use it in GitHub Desktop.
Async read Excel file from browser
// SheetJS should be loaded (https://unpkg.com/xlsx/dist/xlsx.full.min.js)
new Promise((resolve, reject) => {
const fileInput = document.createElement("input")
fileInput.type='file'
fileInput.style.display='none'
fileInput.onchange=evt => {
const reader = new FileReader();
reader.readAsBinaryString(evt.target.files[0])
reader.onload = evt2 => {
const workbook = XLSX.read(evt2.target.result, {type: "binary"})
console.log(workbook)
resolve(JSON.stringify(XLSX.utils.sheet_to_json(workbook.Sheets.Raw)))
}
}
document.body.appendChild(fileInput)
fileInput.click()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment