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

    728x90
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€