xlsx-js-style λ₯Ό μ΄μ©ν table μμλ‘ μμ νμΌ λ€μ΄λ‘λ μν€κΈ°
μ°Έκ³ : 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