You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Stringvardatas="[[고객코드, 매장코드, 고객 전화번호], [0, AAAA, 01011112222], [1, AAAA, 01011112222], [2, ABCD, 01011112222], [3, AAAA, 01022223333], [4, BBBB, 01022223333]]";// 2차원 배열(Object)vararray=arrayTo2DArray(strToArray(excelDatas),3);// 문자열 to 1차원 arrayfunctionstrToArray(str){str=str.replace(/[\[\]']+/g,'');vararray=[];array=str.split(',');for(vari=0;i<array.length;i++){array[i]=array[i].trim();}returnarray;}// 1차원 array to 2차원 arrayfunctionarrayTo2DArray(array,count){newArray=[];while(array.length>0){newArray.push(array.splice(0,count));}returnnewArray;}
SheetJS
JS에서 엑셀 Read 및 Write 하는 매우 간단하고 편리한 오픈소스
<!--필수,SheetJS--><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.3/xlsx.full.min.js"></script><!--필수,FileSaversavaAs이용--><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>...// Excel writefunctionexportExcel(){varworkbook=XLSX.utils.book_new();workbook.SheetNames.push("sheet1");// array => 위 설명에 있는 2차원 배열, 엑셀 sheet에 write할 데이터varwsData=this.array;// aoa => array of array, Json 등 다른 방식도 가능varworksheet=XLSX.utils.aoa_to_sheet(wsData);// 시트 데이터를 시트에 넣기 (시트 명이 없는 시트인경우 첫번째 시트에 데이터가 들어감 )workbook.Sheets["sheet1"]=worksheet;// 엑셀 파일 쓰기varexportWorkbook=XLSX.write(workbook,{bookType:'xlsx',type: 'binary'});// 파일 다운로드saveAs(newBlob([s2ab(exportWorkbook)],{type:"application/octet-stream"}),excelFileName);}functions2ab(datas){varbuf=newArrayBuffer(datas.length);varview=newUint8Array(buf);//create uint8array as viewerfor(vari=0;i<datas.length;i++)view[i]=datas.charCodeAt(i)&0xFF;//convert to octetreturnbuf;}
JavaScript 2차원 배열 형식의 문자열을 2차원 배열로 변환하기
SheetJS
References
https://github.com/SheetJS
https://redstapler.co/sheetjs-tutorial-create-xlsx/
The text was updated successfully, but these errors were encountered: