πŸ“œJS

xlsx-js-style λ₯Ό μ΄μš©ν•œ table μš”μ†Œλ‘œ μ—‘μ…€ 파일 λ‹€μš΄λ‘œλ“œ μ‹œν‚€κΈ°

harry.93 2021. 11. 5. 16:22
λ°˜μ‘ν˜•

μ°Έκ³  : 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

728x90
λ°˜μ‘ν˜•