반응형
electron app 에서 excel 파일 읽기.
excel file read at electron app.
electron app 에서 excel 파일을 읽어 화면에 뿌리는 예제입니다.
추후에 좀 더 심화 하기로 하겠습니다.
여기서 사용한 library 는 sheetjs 입니다.
우선 index.html에서만 작업을 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <!DOCTYPE html> <html> <head> <script src="https://unpkg.com/xlsx@0.11.18/dist/xlsx.full.min.js"></script> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> We are using node <script>document.write(process.versions.node)</script>, Chrome <script>document.write(process.versions.chrome)</script>, and Electron <script>document.write(process.versions.electron)</script>. <input type="file" id="selfile" accept=".xlsx, .txt"> <button id="btn1" value="Get Excel Data" onclick="getExcelFile()">Click</button> <div id="grid"> </div> </body> </html> <script> function getExcelFile() { const fObj = document.getElementById("selfile"); console.log(fObj.value); if(fObj.value === '') { alert('Please Select File'); } else { const selectedFile = fObj.files[0]; // console.log(selectedFile.path); // console.log('Name :' + selectedFile.name + '/ Size : ' + selectedFile.size); var reader = new FileReader(); reader.onload = function(evt) { if(evt.target.readyState == FileReader.DONE) { var data = evt.target.result; data = new Uint8Array(data); // call 'xlsx' to read the file var workbook = XLSX.read(data, {type: 'array'}); // console.log(workbook); var toHtml = XLSX.utils.sheet_to_html(workbook.Sheets['Sheet1'], {header:1}); document.getElementById('grid').innerHTML = toHtml; } }; reader.readAsArrayBuffer(selectedFile); } } </script> | cs |
이렇게 하면 결과가 출력됩니다.
간단한 excel 파일을 넣어봤습니다.
반응형
'엉터리 개발 이야기 > electron' 카테고리의 다른 글
[electron] bootstrap 적용하기 (0) | 2018.02.09 |
---|---|
[electron] electron + jquery load (0) | 2018.02.08 |
[electron] webstorm 에서 electron debug 설정 (0) | 2018.01.24 |
[electron] electron 무작정 따라하기(hello world) (0) | 2018.01.24 |