์ฐธ๊ณ : https://programmer.help/blogs/export-excel-using-js-xlsx-pure-front-end.html
Export excel using js-xlsx pure front end
Preface Recently, the company needs to export several statistical tables to excel. Because the company's existing export excel function is front-end and back-end export, it is troublesome, so I want to find a pure front-end export tool, and finally found j
programmer.help
1. xlsx-js-style ์์ "xlsx.bundle.js" ํ์ผ import
https://github.com/gitbrent/xlsx-js-style
dist ํด๋ ๋ด์์ ํด๋น ํ์ผ์ ๊ฐ์ ธ์จ๋ค.
GitHub - gitbrent/xlsx-js-style: SheetJS Community Edition + Basic Cell Styles
SheetJS Community Edition + Basic Cell Styles. Contribute to gitbrent/xlsx-js-style development by creating an account on GitHub.
github.com
2. "xlsx.extendscript.js" ํ์ผ import
https://www.cdnpkg.com/xlsx/file/xlsx.extendscript.js/?id=78603
ํด๋น ํ์ผ์ XLSX ๋ชจ๋์ ํ์ฅํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํด์ค๋ค.
XLSX.EXTENDSCRIPT.JS: DOWNLOAD - CDNPKG
xlsx.extendscript.js is available in 31 versions of xlsx. 0.15.6 0.15.5 0.15.4 0.15.1 0.15.0 0.14.5 0.14.4 0.14.3 0.14.2 0.14.1 0.14.0 0.13.5 0.13.4 0.13.3 0.13.2 0.13.1 0.13.0 0.12.13 0.12.12 0.12.11 0.12.10 0.12.9 0.12.8 0.12.7 0.12.6 0.12.5 0.12.4 0.12.
www.cdnpkg.com
3. ํด๋น ํ์ด์ง์ import
<script type="text/javascript" src="/content/02comm/js/xlsx.bundle.js"></script>
<script type="text/javascript" src="/content/02comm/js/xlsx.extendscript.js"></script>
xlsx-js ๋ฅผ ์ด์ฉํ์ฌ ์์ ํ์ผ๋ก ๋ง๋๋ ๋ฐฉ์์๋ JSON / ARRAY ๋ฑ ์ฌ๋ฌ ๋ฐฉ์์ด ์์ผ๋ ์ฌ๊ธฐ์๋ HTML ์์์ธ table ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๊ณต์ ํ๊ณ ์ ํ๋ค.
1. ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ํ ์ด๋ธ -> ์์ ์์ฑ
- html
<a href="#" class="btn01 col04" title="์์
๋ค์ด๋ก๋" onclick="test1();">
<span>์์
๋ค์ด๋ก๋1</span>
</a>
<div class="table_wrap pc_view">
<table class="table t_list" id="test">
<thead>
<tr>
<th>ํญ๋ชฉ1</th>
<th>ํญ๋ชฉ2</th>
<th>ํญ๋ชฉ3</th>
<th>ํญ๋ชฉ4</th>
<th>ํญ๋ชฉ5</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tbody>
</tbody>
</table>
</div>
- js
function test1() {
// workBook Create
var wb = XLSX.utils.book_new();
// workSheet Create
var ws = XLSX.utils.table_to_sheet( document.getElementById( 'test' ));
// Sheet Append With Title
XLSX.utils.book_append_sheet( wb ,ws ,'sheet title' );
// Create Excel File With File Name
XLSX.writeFile( wb ,( 'ํ์ผ๋ช
.xlsx' ) );
}
- ๊ฒฐ๊ณผ
2. ํ ์ด๋ธ์ ์คํ์ผ ์ถ๊ฐํ๊ธฐ
์ฌ๊ธฐ์๋ถํฐ๋ ํจ์ ๋ด์ฉ๋ง ๋ณ๊ฒฝ๋ฉ๋๋ค.
- js
function test2() {
// workBook Create
var wb = XLSX.utils.book_new();
// workSheet Create
var ws = XLSX.utils.table_to_sheet( document.getElementById( 'test' ));
// for in Cells add style
for ( i in ws ) {
if ( typeof ( ws[ i ] ) != "object" ) continue;
let cell = XLSX.utils.decode_cell( i );
// add Style to All Cells
ws[ i ].s = {
font : {
name : "arial"
} ,
alignment : {
vertical : "center" ,
horizontal : "center" ,
wrapText : '1' ,
} ,
border : {
right : {
style : "thin" ,
color : "000000"
} ,
left : {
style : "thin" ,
color : "000000"
} ,
top : {
style : "thin" ,
color : "000000"
} ,
bottom : {
style : "thin" ,
color : "000000"
} ,
}
};
// Cell format change
/* if ( cell.c == 0 ) { // first column
ws[ i ].s.numFmt = "DD/MM/YYYY HH:MM"; // for dates
ws[ i ].z = "DD/MM/YYYY HH:MM";
} else {
ws[ i ].s.numFmt = "00.00"; // other numbers
} */
// First row is filled gray bg
if ( cell.r == 0 ) {
ws[ i ].s.fill = {
patternType : "solid" ,
fgColor : {
rgb : "b2b2b2"
} ,
bgColor : {
rgb : "b2b2b2"
}
};
}
}
// Sheet Append With Title
XLSX.utils.book_append_sheet( wb ,ws ,'sheet title' );
// Create Excel File With File Name
XLSX.writeFile( wb ,( 'ํ์ผ๋ช
.xlsx' ) );
}
- ๊ฒฐ๊ณผ
3. ํ์ดํ ROW ์ถ๊ฐ ๋ฐ Cell Merge ๋ฑ, ๋ณตํฉ์์ ์ถ๊ฐ
์ด ๋ถ๋ถ์์ xlsx.extendscript.js ๋ฅผ ์์ ํ๊ฒ ๋ฉ๋๋ค.
ํ ์ด๋ธ์ ์ํธ๋ก ๋ง๋๋ ๊ณผ์ ์์ ๋ช๊ฐ์ง ์ปค์คํ ๊ณผ์ ์ ์งํํฉ๋๋ค.
- xlsx.extendscript.js ์์ ํ๊ธฐ
1. XLSX ๋ณ์๋ฅผ ๊ธฐ์กด ๋ณ์์ ๊ฒน์น์น ์๊ณ ์ฌ์ฉํ๊ธฐ ์ํด ๋ณ์๋ช ์ ๋ณ๊ฒฝ
ํด๋น ํ์ผ 9160 ๋ผ์ธ ๊ทผ์ฒ์ "var XLSX" ๋ก ์ ์ธ๋๋ ๋ณ์๊ฐ ์์ต๋๋ค.
ํด๋น ๋ณ์๋ฅผ XLSX2 ๋ก ๋ณ๊ฒฝํ๊ณ , ์ดํ ๋์ค๋ ๋ณ์๋ค์ ์ ๋ถ XLSX2 ๋ก ๋ณ๊ฒฝํฉ๋๋ค.
2. parse_dom_table ํจ์ ๋ณ๊ฒฝ
ํด๋น ํจ์๋ ํ ์ด๋ธ์ ๊ฐ์ ธ์ ํ์ฑํ๋ ๋ถ๋ถ์ธ๋ฐ ์ต์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์์ ํฉ๋๋ค.
ํด๋น ์ต์ ์๋ rowIndex ์ merge ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๊ธฐ ์ํจ์ ๋๋ค.
- ๋ณ๊ฒฝ ์
- ๋ณ๊ฒฝ ํ
์ ์์ค ์์ ์์ ์ ํตํด ์ปค์คํ ํ ํ ์ ์๊ฒ ๋์์ต๋๋ค.
์ดํ, ํ์ฑํ๋ ๊ณผ์ ์์ XLSX2 ๋ณ์๋ฅผ ํตํด ์ฌ์ฉํด์ฃผ๋ฉด ๋ฉ๋๋ค.
- js
function test3 () {
// Create WorkBook
var wb = XLSX.utils.book_new();
// Define Option
var opt = {
// new row except table element
rowIndex : 1 ,
// merge option ( if you have to merge new row )
merges : [ {
// start
s : {
c : 0 , // col
r : 0 // row
} ,
// end
e : {
c : $( "#test" ).find( "th" ).length - 1 , // col ( this means : merge as table column counts )
r : 0 // row
}
} ] ,
};
// WorkSheet
var ws = XLSX2.utils.table_to_sheet( document.getElementById( 'test' ) ,opt );
// new row --> Title
ws[ "A1" ] = {
t : "s" ,
v : "CUSTOM TITLE"
};
// new row --> style
ws[ "A1" ].s = {
font : {
name : "arial" ,
bold : true ,
} ,
alignment : {
vertical : "center" ,
horizontal : "center" ,
wrapText : '1' , // any truthy value here
} ,
border : {
right : {
style : "thin" ,
color : "000000"
} ,
left : {
style : "thin" ,
color : "000000"
} ,
top : {
style : "thin" ,
color : "000000"
} ,
} ,
};
// cell style
for ( i in ws ) {
if ( typeof ( ws[ i ] ) != "object" ) continue;
let cell = XLSX.utils.decode_cell( i );
// cell style
console.log(cell)
console.log(i)
console.log(ws[i])
ws[ i ].s = {
font : {
name : "arial"
} ,
alignment : {
vertical : "center" ,
horizontal : "center" ,
wrapText : '1' ,
} ,
border : {
right : {
style : "thin" ,
color : "000000"
} ,
left : {
style : "thin" ,
color : "000000"
} ,
top : {
style : "thin" ,
color : "000000"
} ,
bottom : {
style : "thin" ,
color : "000000"
} ,
}
};
// new row & first row ( table th ) style
if ( cell.r == 0 || cell.r == 1 ) {
ws[ i ].s.fill = {
patternType : "solid" ,
fgColor : {
rgb : "b2b2b2"
} ,
bgColor : {
rgb : "b2b2b2"
}
};
}
// if you merge other rows use this
/* if ( i == "!merges" ) {
ws[ "!merges" ].push( {
s : {
c : 0 ,
r : 0
} ,
e : {
c : 0 ,
r : 0
}
} );
} */
}
// Sheet Append With Title
XLSX.utils.book_append_sheet( wb ,ws ,'sheet title' );
// Create Excel File With File Name
XLSX.writeFile( wb ,( 'ํ์ผ๋ช
.xlsx' ) );
}
- ๊ฒฐ๊ณผ
์ ํ์ ๋ณ๊ฒฝ์ด๋ ๊ธฐํ ๋ค๋ฅธ ์ต์ ๋ ๋ง์ผ๋ ๋ ์์ธํ ๋ถ๋ถ์ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒ์ํ์ฌ ์ถ๊ฐํด๋ณด์๋ฉด์ ํ๋ฉด ๋น ๋ฅด๊ฒ ํ์ค ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค !
์ด์ ์ ๋๋ค. :D
'๐JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ToastUI Editor] ์ด๋ฏธ์ง ๋ณต์ฌ ๋ถ์ฌ ๋ฃ๊ธฐ ์, ํ์ผ ์ ๋ก๋ ๋ฐ UI์ฒ๋ฆฌ (0) | 2022.03.29 |
---|---|
[ IOS ] invalid Date ๊ด๋ จ ๋ฌธ์ (0) | 2022.03.16 |
[Javascript] Promise ์ดํดํ๊ธฐ (0) | 2021.10.06 |
String Array ๋ด ๋น ์์ ์ ๊ฑฐํ๊ธฐ (0) | 2021.02.06 |
์คํฌ๋ฆฝํธ ํ์ผ import ์, ์บ์ ์๋จ๊ฒ ์ฒ๋ฆฌํ๊ธฐ (0) | 2021.02.06 |
๋๊ธ